JavaScript supports different types of values that can be stored and manipulated in variables. These values are classified into two main categories:
These are immutable (cannot be changed) and stored directly in memory.
Data Type | Description | Example |
---|---|---|
String | Text enclosed in quotes | "Hello" or 'World' |
Number | Numeric values (integer/float) | 42 , 3.14 , -10 |
Boolean | Logical values (true or false ) |
true , false |
Undefined | Variable declared but not assigned a value | let x; → undefined |
Null | Represents an empty or unknown value | let y = null; |
BigInt | Large integers beyond Number limit |
123456789012345678901234567890n |
Symbol | Unique and immutable value, used as object keys | Symbol("id") |
let name = "John"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let value; // Undefined
let emptyValue = null; // Null
let bigNum = 12345678901234567890n; // BigInt
let id = Symbol("unique"); // Symbol
console.log(typeof name, typeof age, typeof isStudent); // Output: string number boolean
These are mutable and stored by reference in memory.
Data Type | Description | Example |
---|---|---|
Object | Collection of key-value pairs | {name: "Alice", age: 30} |
Array | Ordered list of values | [10, 20, 30] |
Function | Block of reusable code | function greet() {} |
let person = { name: "Alice", age: 30 }; // Object
let numbers = [10, 20, 30]; // Array
function greet() {
return "Hello!";
} // Function
console.log(person.name, numbers[1], greet()); // Output: Alice 20 Hello!
typeof
)You can check the data type of a variable using the typeof
operator.
console.log(typeof "Hello"); // Output: string
console.log(typeof 42); // Output: number
console.log(typeof true); // Output: boolean
console.log(typeof undefined); // Output: undefined
console.log(typeof null); // Output: object (JavaScript bug)
console.log(typeof [1, 2, 3]); // Output: object (Arrays are objects)
console.log(typeof function() {}); // Output: function
🔹 Note: typeof null
returns "object"
due to a known JavaScript bug.
typeof
operator helps identify the data type of a variable.