Pass by Value vs. Pass by Reference
Understanding how JavaScript handles primitive and non-primitive types when passing them to functions or assigning them to variables.
Key concepts
Pass by Value
Primitive types are copied by value. Modifications do not affect the original variable.
Pass by Reference
Non-primitive types (objects, arrays) are passed by reference. Modifications affect the original.
Examples
Pass by Value:
let x = 10;
let y = x;
y += 5;
console.log(x); // 10
console.log(y); // 15Pass by Reference:
const obj1 = { name: 'Alice' };
const obj2 = obj1;
obj2.name = 'Bob';
console.log(obj1.name); // Bob (shared reference)Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus