JavaScript data types are physically divided into two categories based on where they are stored in the architecture.
These are immutable data types that fit directly inside a single Stack Memory Slot.
number, string, boolean, undefined, null, symbol, bigint.These are mutable collections that live in the Heap.
Reference.Payload (The key-value pairs).Object, Array, Function, Date, Map, Set, WeakMap.In lower-level languages (C, Java), an Array is a contiguous block of reserved memory. In JavaScript, it is not.
Physically, a JavaScript Array is just a specialized Object in the Heap.
arr[0] = "A", the engine treats it almost exactly like obj["0"] = "A". The indices are treated as string keys internally.length property.Because Arrays are just Objects (Key-Value maps), they don't need to allocate memory for empty slots.
Consider a scenario where we initialize an empty array and assign values only at index 0 and index 1000:
Physical Reality: The Engine does NOT reserve 1001 memory slots. It creates an Object with exactly two keys: 0 and 1000.
The Gap: Indices 1 through 999 simply do not exist in memory. They are holes. Accessing arr[50] returns undefined not because the slot is empty, but because the Key Lookup Failed (similar to accessing a missing property on an object).
| Feature | Primitives | Objects | Arrays |
|---|---|---|---|
| Storage Location | Stack | Heap | Heap |
| Variable Holds | The Value | The Address (Pointer) | The Address (Pointer) |
| Mutability | Immutable | Mutable | Mutable |
| Structure | Fixed Size | Dynamic Graph | Hash Map (Key-Value) |
| Lookup Cost | Instant | 1 Pointer Jump | Key Hashing + Pointer Jump |