Reference Types
Reference Types
Section titled “Reference Types”Introduction
Section titled “Introduction”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.
Why Do We Need This?
Section titled “Why Do We Need This?”Reference types allow us to build complex data structures. Without them, we couldn’t create arrays, objects, or reusable functions.
Real World Story
Section titled “Real World Story”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.
Visual Explanation
Section titled “Visual Explanation”flowchart LR A[Variable: obj1] --> B["Reference: 0xFF"] B --> C["Heap: { name: 'Alice' }"] D[Variable: obj2] --> BBoth obj1 and obj2 point to the same object in memory.
Detailed Explanation
Section titled “Detailed Explanation”Objects
Section titled “Objects”const person = { name: 'Alice', age: 25};Arrays
Section titled “Arrays”const numbers = [1, 2, 3];Functions
Section titled “Functions”function greet(name) { return `Hello, ${name}`;}Pass by Reference
Section titled “Pass by Reference”function updateName(obj) { obj.name = 'Bob';}
const user = { name: 'Alice' };updateName(user);console.log(user.name); // 'Bob' — modified!Internal Working
Section titled “Internal Working”flowchart LR subgraph Stack A[ref1: 0x001] B[ref2: 0x001] end subgraph Heap C["0x001: { name: 'Alice' }"] end A --> C B --> CBest Practices
Section titled “Best Practices”- Use
Object.freeze()to make objects immutable - Create copies using spread (
...) orObject.assign() - Be careful when passing objects to functions
Common Mistakes
Section titled “Common Mistakes”const obj1 = { value: 1 };const obj2 = obj1;obj2.value = 2;console.log(obj1.value); // 2 — unexpected mutation!Summary
Section titled “Summary”- Reference types store memory addresses
- Objects, arrays, and functions are reference types
- Passed by reference — mutations affect all references