| Feature | Primitive Data Types | Non-Primitive Data Types |
|--------------------|-----------------------------=-----------------------|--------------------------------------------------------|
| Examples | Number String Boolean undefined null BigInt Symbol| Object, Array, Function, Date, etc. |
| Mutability | ❌ Immutable – cannot be changed | ✅ Mutable – can be modified |
| Memory Location| Stored in Stack | Stored in Heap, reference is stored in Stack |
| Copy Behavior | 📋 Copied by value | 🔗 Copied by reference |
| Comparison | Compared by value | Compared by reference (memory address) |
| Size in Memory | Fixed size, lightweight | Dynamic size, heavier |
| Used for | Storing single, simple values | Storing collections, structured data |
| Example Code | let x = 10; let y = x; → x ≠ y after change | let a = [1,2]; let b = a; → a == b (same reference)|
In JavaScript, primitive data types are stored in the stack memory because they are immutable and have a fixed size.
- Represents textual data.
- Enclosed in single (
'), double ("), or backticks (`). - Example:
let name = "Vishnu"; let greeting = 'Hello'; let templateString = `Hello, ${name}`;
- Represents both integers and floating-point numbers.
- Example:
let age = 25; let price = 99.99; let infinityValue = Infinity; let notANumber = NaN;
- Used for large numbers beyond
Number.MAX_SAFE_INTEGER. - Created by adding
nto the end of an integer. - Example:
let bigNumber = 9007199254740991n;
- Represents
trueorfalsevalues. - Example:
let isLoggedIn = true; let hasPermission = false;
- A variable that has been declared but not assigned a value.
- Represents an uninitialized variable or obsence of a value
- Example:
let user; console.log(user); // undefined
- Represents an intentional absence of value.
- Represents the intentional absence of any object value
- Example:
let emptyValue = null;
- Used to create unique values.
- Example:
let uniqueKey = Symbol("id");
- All primitive types are immutable (cannot be changed after creation).
typeofoperator can be used to check data types.nullis an object due to a historical JavaScript bug.
Example:
console.log(typeof "Hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof null); // "object"
console.log(typeof undefined); // "undefined"
console.log(typeof Symbol("id")); // "symbol"Non-primitive data types are stored in the heap, and their reference (memory address) is stored in the stack. They are mutable and used for storing collections or complex data.
- A collection of key-value pairs.
- Example:
const person = { name: "Vishnu", age: 22 };
- Ordered collection of values (indexed).
- Example:
const numbers = [1, 2, 3];
- A block of reusable code.
- Example:
function greet() { console.log("Hello World"); }
- Built-in objects for special purposes.
- Example:
const today = new Date(); const pattern = /abc/; const map = new Map(); const set = new Set();
- All non-primitive types are objects internally.
- When you copy a non-primitive variable, you copy the reference, not the value.
- Any changes via one reference affect all references.
Example:
let a = [1, 2, 3];
let b = a;
b.push(4);
console.log(a); // [1, 2, 3, 4] – a is affected tooUse Array.isArray() or typeof to distinguish between objects and arrays/functions:
console.log(typeof {}); // "object"
console.log(Array.isArray([])); // true
console.log(typeof function() {}); // "function"