--> Sayadasite: Data Types in Javascript

Multiple Ads

Search

Menu Bar

Data Types in Javascript

Client side Java script Server side Java script Data types  variables Operators  Expressions  Functions  Objects  Array  Date and math related objects Document objectmodel Event handling

JavaScript supports various data types, which can be broadly categorized into primitive and non-primitive types.

Primitive Data types

Primitive data types represent single values and are immutable. (not changing)

Numeric Type

1. Number: Represents numeric values (integers and decimals).

let n = 42;
let pi = 3.14;

2. BigInt: Represents integers larger than Number.MAX_SAFE_INTEGER.

let bigNumber = 123456789012345678901234567890n;

let x = 1234567890123456789012345n;
let y = BigInt(1234567890123456789012345)

This addresses a limitation of the standard Number type, which can only safely represent integers within the range of Number.MAX_SAFE_INTEGER (2^53 - 1) and Number.MIN_SAFE_INTEGER (-(2^53 - 1)). Beyond this range, Number values can lose precision, leading to incorrect calculations and comparisons. A primitive data type that can represent integers of any size, limited only by available memory. 9007199254740991. And -(9007199254740991)

 

Non-Numeric Type

1. String: Represents text enclosed in single or double quotes.

let s = "Hello, World!";

let color = "Yellow";
let lastName = "Johnson";

2. Boolean: Represents a logical value (true or false).

let bool= true;

let x = true;
let y = false;

3. Null: Represents an intentional absence of any value.

let empty = null;

let x = null;
let y = null;

4. Undefined: A variable that has been declared but not assigned a value.

let notAssigned;

console.log(notAssigned);

let x;
let y;

Output

undefined

5. Symbol: Represents unique and immutable values, often used as object keys.

let sym = Symbol('unique');

const x = Symbol();
const y = Symbol();

Non-Primitive Datatypes

Non-primitive types are objects and can store collections of data or more complex entities.

1. Object: Represents key-value pairs.

Object: The most fundamental non-primitive type. Objects are collections of key-value pairs, where keys are typically strings (or Symbols) and values can be any data type, including other objects. They are used to represent more complex entities and structures.

Objects are collections of key-value pairs, enclosed in curly braces {}. Each key is a string (or a Symbol), and it maps to a value of any data type.

In JavaScript, both objects and arrays are used to store collections of data, but they differ in their structure, organization, and primary use cases.

Syntax:

const objectName = {
  key1: value1,
  key2: value2,
  // ...
  keyN: valueN
};

Example

   const person = {
        name: "Ali",
        age: 30,
        occupation: "Engineer"
    };

Example

let obj = {
    name: "Amit",
    age: 25
};

Objects

In JavaScript, an Object is a variable that can hold many variables. An object is a non-primitive data type used to store collections of data as key-value pairs, where each key (known as property names) has a value. Objects can describe anything like houses, cars, people, animals, or any other subjects. It serves as a fundamental building block for organizing and managing related information and behavior.

Example

Car Object

 

Car Properties

Car Methods

car.name = Fiat

car.model = 500

car.weight = 850kg

car.color = white

car.start()

car.drive()

car.brake()

car.stop()

JavaScript Objects

This code assigns many values (Fiat, 500, white) to an object named car:

Example

1 const car = {type:"Fiat", model:"500", color:"white"};

start: function() {
    console.log("The car is starting.");

 

In JavaScript, console. log() method is a built-in JavaScript function that outputs messages to the console 

 

2 const car = {
  type: "Fiat",
  model: "500",
  color: "white",
  start: function() {
    console.log("The car is starting.");
  },
  drive: function() {
    console.log("The car is driving.");
  }
};

No comments: