In JavaScript, there are two sorts of data types -
Data Type | Description |
---|---|
Number | Integers or floating-point |
String | Series of one or more characters |
Boolean | True or False |
Undefined | Unassigned values |
Null | Unknown values |
Data Type | Description |
---|---|
Object | Key:value pairs |
Array | Group of similar values |
let value = 85; // Number
let name = "Vishal Torgal"; // String
let bigInt = 56789895678989567898956789895678989; // Bigint
let booleanValue = ture; // Boolean
let value = undefined; // Undefined
let value = null; // Null
let val; // Now val is undefined
val = "Torgal"; // Now val is a String
val = 5; // Now val is a Number
// key-value pairs collection
var obj = {
no: 1,
country: "India"
}
// Ordered list collection
var arr = [1, true, "India"];
The conversion of values between different data types is known as type coercion. Expressions in JavaScript are evaluated from left to right.
// Converted to string
let val = 20 + "aura";
console.log(val); // 20aura
// Before reaching "aura" string, 20 and 5 were considered as number.
let val = 20 + 5 + "aura";
console.log(val); // 25aura
// Converted to string
let val = "hello" + 20 + 5;
console.log(val); // hello205
// With single quotes:
let val = 'Aura';
// With double quotes:
let val = "Hello";
let val1 = "Hello 'Aura'";
console.log(val1) // Hello 'Aura'
let val2 = 'Learning "Javascript"';
console.log(val2) // Learning "Javascript"
let val1 = 99e7;
console.log(val1) // 990000000
let val2 = 99e-7;
console.log(val2) // 0.0000099
It is used to determine the data type of its operand.
typeof 85 // "number"
typeof "Torgal" // "string"
typeof false // "boolean"
typeof 4342371323421391137011120n // "bigint"
typeof NaN // "number"
Represents values is not defined.
let val;
console.log(val); // undefined
Represents empty or unknown value. NULL or Null is not same as null.
let val = null;
console.log(val); // null