In JavaScript, the way arguments are passed depends on data type.
Technically, JavaScript is call by value, but for objects it behaves like call by reference (more accurately: call by sharing).
🔹 Call by Value
A copy of the value is passed to the function.
Changes inside the function do NOT affect the original variable.
Example (Primitive types)
let a = 10;
function change(x) {
x = 20;
}
change(a);
console.log(a); // 10
🧠 Explanation
a(10) is copied intox- Modifying
xdoesn’t changea
✔ Applies to: number, string, boolean, null, undefined, symbol, bigint
🔹 Call by Reference (Conceptual)
A reference to the same memory is passed.
Changes inside the function affect the original object.
Example (Objects & Arrays)
let obj = { name: "Teekam" };
function update(o) {
o.name = "React";
}
update(obj);
console.log(obj.name); // React
🧠 Explanation
- Both
oandobjpoint to the same object - Mutation affects original data
⚠️ Important Interview Truth
JavaScript is NOT truly call by reference.
It is call by value, where:
- For primitives → value is copied
- For objects → reference value is copied
This is called call by sharing.
🔍 Proof (Key Trick Question)
let obj = { x: 1 };
function change(o) {
o = { x: 2 };
}
change(obj);
console.log(obj.x); // 1
Why?
- Reference is passed by value
- Reassigning
odoes NOT affectobj
🔥 Summary Table
| Type | Behavior |
|---|---|
| Primitive | Call by value |
| Object | Reference passed by value |
| Object mutation | Affects original |
| Object reassignment | Does NOT affect original |
🎯 Short Interview Answer
JavaScript uses call by value. For primitives, the actual value is copied. For objects, a reference value is copied, so mutating the object affects the original, but reassigning the object does not.
⭐ One-line summary
JavaScript is call by value — objects just pass a reference value, not true references.