Data Types
1. Primitive Types
Stored by value
Types:
- String →
"hello" - Number →
10 - Boolean →
true / false - Undefined → variable declared but no value
- Null → intentionally empty
Example:
let name = "blackbox"; // String let age = 20; // Number let isStudent = true; // Boolean let x; // Undefined let y = null; // Null
2. Reference Types
👉 Stored by reference (memory address)
Types:
- Object
- Array
- Function
Example:
let user = {
name: "blackbox",
age: 20
};
let arr = [1, 2, 3];
Key Difference (VERY IMPORTANT)
Primitive:
let a = 10; let b = a; b = 20; console.log(a); // 10
👉 Copy created → separate memory
Reference:
let obj1 = { name: "John" };
let obj2 = obj1;
obj2.name = "Alex";
console.log(obj1.name); // Alex 😱
👉 Same memory → both change