Skip to content

Reference Types

Reference types store references (memory addresses) to values. Unlike primitives, they are mutable and stored in the heap. Objects, arrays, and functions are all reference types.

Reference types allow us to build complex data structures. Without them, we couldn’t create arrays, objects, or reusable functions.

A reference type is like a house address. Multiple people can have the same address, and if someone renovates the house, everyone sees the change. Primitives are like a photograph — each copy is independent.

flowchart LR
A[Variable: obj1] --> B["Reference: 0xFF"]
B --> C["Heap: { name: 'Alice' }"]
D[Variable: obj2] --> B

Both obj1 and obj2 point to the same object in memory.

const person = {
name: 'Alice',
age: 25
};
const numbers = [1, 2, 3];
function greet(name) {
return `Hello, ${name}`;
}
function updateName(obj) {
obj.name = 'Bob';
}
const user = { name: 'Alice' };
updateName(user);
console.log(user.name); // 'Bob' — modified!
flowchart LR
subgraph Stack
A[ref1: 0x001]
B[ref2: 0x001]
end
subgraph Heap
C["0x001: { name: 'Alice' }"]
end
A --> C
B --> C
  • Use Object.freeze() to make objects immutable
  • Create copies using spread (...) or Object.assign()
  • Be careful when passing objects to functions
const obj1 = { value: 1 };
const obj2 = obj1;
obj2.value = 2;
console.log(obj1.value); // 2 — unexpected mutation!
  • Reference types store memory addresses
  • Objects, arrays, and functions are reference types
  • Passed by reference — mutations affect all references