JavaScript Call by Value vs Reference Explained

🚀 Call by Value vs Call by Reference — Every JavaScript Developer Must Understand This One of the most commonly asked interview questions — yet many developers explain it incorrectly. Let’s simplify it 👇 🔹 Call by Value When you pass a primitive type (number, string, boolean, null, undefined, symbol, bigint), JavaScript copies the value. let a = 10; function update(x) { x = 20; } update(a); console.log(a); // 10 👉 The original variable does NOT change. Because a copy was passed. 🔹 Call by Reference (Not Exactly 😉) When you pass an object, array, or function, JavaScript passes the reference to the memory location. let user = { name: "Sweta" }; function update(obj) { obj.name = "Anita"; } update(user); console.log(user.name); // Anita 👉 The original object changes. Because both variables point to the same memory reference. ⚠️ Important Clarification JavaScript is technically: ✅ Pass by Value But for objects, the value itself is a reference. That’s why many people say “call by reference” — but internally it’s still pass-by-value (of the reference). 🔥 Real Interview Tip If interviewer asks: “Is JavaScript pass by value or pass by reference?” Best answer: JavaScript is pass-by-value. For objects, the value being passed is a reference to the object. 🎯 This shows conceptual clarity. 💡 Why This Matters in Real Projects? Prevents accidental state mutation in React/Vue Helps avoid bugs in Redux/Pinia Essential for understanding immutability Critical when working with micro-frontends & shared state Understanding this deeply makes you a better JavaScript engineer — not just someone who writes syntax. #JavaScript #FrontendDevelopment #ReactJS #VueJS #InterviewPreparation #WebDevelopment

To view or add a comment, sign in

Explore content categories