🚀 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
JavaScript Call by Value vs Reference Explained
More Relevant Posts
-
JavaScript Notes: From Fundamentals to Advanced Concepts (Interview & Production Ready) These JavaScript notes are a structured, practical, and interview-oriented collection of concepts that every frontend and full-stack developer must understand deeply, not just memorize. Instead of surface-level definitions, these notes focus on how JavaScript actually works under the hood, why certain bugs occur, and how JS behaviour affects React performance, scalability, and real-world production applications. The content is built from: Real interview questions Debugging experience from real projects Common mistakes developers make even after years of experience What these notes cover JavaScript Fundamentals Execution context & call stack Scope, lexical environment & scope chain var, let, const (memory & hoisting differences) Hoisting explained with execution flow Core JavaScript Concepts this keyword (implicit, explicit, arrow functions) Closures (memory behaviour & real use cases) Prototypes & prototypal inheritance Shallow vs deep copy Reference vs value Asynchronous JavaScript Callbacks & callback hell Promises (microtask queue behaviour) Async/Await (what actually pauses execution) Event loop, microtasks vs macrotasks Real execution order questions asked in interviews Advanced & Interview-Critical Topics Debouncing & throttling Currying & function composition Polyfills (map, filter, reduce, bind) Equality operators (== vs ===) Memory leaks & garbage collection basics JavaScript for React Developers Closures inside hooks Reference equality & re-renders Immutability & state updates Async state behaviour Performance pitfalls caused by JS misunderstandings #ReactJS #JavaScriptForReact #FrontendPerformance #Hooks #WebDevelopers
To view or add a comment, sign in
-
What do you think will be the output of these expressions in javascript? {} + [] [] + {} {} + {} [] + [] At first glance, most developers assume JavaScript will treat these as normal object or array operations. But the real reason behind the output is type coercion and how JavaScript converts objects and arrays into primitive values when using the + operator. When the + operator is used with objects or arrays, JavaScript tries to convert them into primitive values (usually strings). An empty array [] becomes an empty string "" An object {} becomes "[object Object]" After this conversion, the + operator performs string concatenation. So the expressions effectively become: "[object Object]" + "" "" + "[object Object]" "[object Object]" + "[object Object]" "" + "" Understanding this behavior is important because JavaScript’s implicit type coercion can sometimes lead to unexpected results in real-world applications. A simple rule to remember: {} → "[object Object]" [] → "" Once you remember this conversion, these puzzles become much easier to reason about. For more insightful content checkout below: 🟦 𝑳𝒊𝒏𝒌𝒆𝒅𝑰𝒏 - https://lnkd.in/dwi3tV83 ⬛ 𝑮𝒊𝒕𝑯𝒖𝒃 - https://lnkd.in/dkW958Tj 🟥 𝒀𝒐𝒖𝑻𝒖𝒃𝒆 - https://lnkd.in/dDig2j75 or Priya Frontend Vlogz 🔷 𝐓𝐰𝐢𝐭𝐭𝐞𝐫 - https://lnkd.in/dyfEuJNt #frontend #javascript #react #reactjs #html #css #typescript #es6 #interviewquestions #interview #interviewpreparation
To view or add a comment, sign in
-
-
🚀 Day 2/30 – Frontend Interview Questions Series Let’s level up your JS fundamentals today 💡 ❓ Q1: What is the difference between "==" and "===" in JavaScript? 👉 "==" (Loose Equality) - Compares values after type conversion - Example: 5 == "5" // true 👉 "===" (Strict Equality) - Compares value + type (no conversion) - Example: 5 === "5" // false ✅ Best Practice: Always use "===" to avoid unexpected bugs. --- ❓ Q2: What is Hoisting in JavaScript? 👉 Hoisting is JavaScript's behavior of moving declarations to the top of their scope. Example: console.log(a); // undefined var a = 10; 🔍 Behind the scenes: var a; console.log(a); a = 10; ⚠️ Note: - "var" is hoisted with "undefined" - "let" and "const" are hoisted but stay in Temporal Dead Zone (TDZ) --- ❓ Q3: What is Closure? 👉 A closure is a function that remembers variables from its outer scope, even after the outer function has finished executing. Example: function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 💡 Closures are widely used in: - Data hiding - Callbacks - React hooks #JavaScript #FrontendDeveloper #InterviewPreparation #ReactJS #WebDevelopment #CodingInterview
To view or add a comment, sign in
-
Great insight Rushikesh Chavhan You can also add more example like what happen when you use arrow function and normal function. also when same function name and variable decaled with var then how hositing happrns.
Front-End Developer @ Laminaar Aviation Infotech | 3 Years Experience | HTML | CSS | JavaScript | React.js | Nodejs | Web Developer | PG-DAC.
🚀 Day 2/30 – Frontend Interview Questions Series Let’s level up your JS fundamentals today 💡 ❓ Q1: What is the difference between "==" and "===" in JavaScript? 👉 "==" (Loose Equality) - Compares values after type conversion - Example: 5 == "5" // true 👉 "===" (Strict Equality) - Compares value + type (no conversion) - Example: 5 === "5" // false ✅ Best Practice: Always use "===" to avoid unexpected bugs. --- ❓ Q2: What is Hoisting in JavaScript? 👉 Hoisting is JavaScript's behavior of moving declarations to the top of their scope. Example: console.log(a); // undefined var a = 10; 🔍 Behind the scenes: var a; console.log(a); a = 10; ⚠️ Note: - "var" is hoisted with "undefined" - "let" and "const" are hoisted but stay in Temporal Dead Zone (TDZ) --- ❓ Q3: What is Closure? 👉 A closure is a function that remembers variables from its outer scope, even after the outer function has finished executing. Example: function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 💡 Closures are widely used in: - Data hiding - Callbacks - React hooks #JavaScript #FrontendDeveloper #InterviewPreparation #ReactJS #WebDevelopment #CodingInterview
To view or add a comment, sign in
-
🚀 JavaScript Interview Questions Every Developer Should Know Here are some useful JS questions with simple answers 👇 🔹 1. What is the output? console.log(typeof null); 👉 Answer: "object" 💡 This is a well-known JavaScript bug. 🔹 2. What is closure? 👉 A closure is a function that remembers variables from its outer scope even after the outer function has finished execution. function outer() { let count = 0; return function inner() { count++; return count; }; } 🔹 3. Difference between == and ===? 👉 == → compares value (loose equality) 👉 === → compares value + type (strict equality) 🔹 4. What is hoisting? 👉 JavaScript moves variable and function declarations to the top of their scope before execution. 🔹 5. What will be the output? let a = 10; (function() { console.log(a); let a = 20; })(); 👉 Answer: ❌ ReferenceError 💡 Due to Temporal Dead Zone (TDZ) 🔹 6. What is event loop? 👉 It handles async operations by managing the call stack and callback queue. 🔹 7. What is this keyword? 👉 Refers to the object that is calling the function (depends on context). 🔹 8. What is a promise? 👉 A promise represents a value that may be available now, later, or never. 🔹 9. What is async/await? 👉 Syntactic sugar over promises to write async code like synchronous code. 🔹 10. What is debounce? 👉 Limits how often a function runs. Useful for search inputs. 🔥 Save this for your next interview 💬 Comment your favorite question 🔁 Share with your developer friends #JavaScript #WebDevelopment #Frontend #InterviewPrep #Coding
To view or add a comment, sign in
-
🔥 Top JavaScript Tricky Interview Questions 🔥 These are usefull for you guys 1️⃣ Why does [] == ![] return true? Because ![] becomes false, then both sides are type-converted. 👉 JS coercion magic. 2️⃣ What will typeof null return? It returns object. 👉 This is a well-known JavaScript bug. 3️⃣ Output of: console.log(1 + "2" + 3) Result: "123" 👉 String concatenation happens left to right. 4️⃣ Output of: console.log(0.1 + 0.2 === 0.3) Result: false 👉 Floating-point precision issue. 5️⃣ Why does NaN !== NaN? Because NaN is not equal to anything — even itself. 👉 Use isNaN() to check. 6️⃣ What is the output of: console.log(typeof NaN)? Result: "number" 👉 Another classic trap. 7️⃣ Difference between null and undefined undefined → variable declared but not assigned null → explicitly assigned empty value 👉 Intent matters. 8️⃣ What happens if you forget await in async function? Function continues execution without waiting. 👉 Can cause silent bugs. 9️⃣ Output of: console.log([] + []) Result: "" (empty string) 👉 Arrays are converted to strings. 🔟 Why is setTimeout(fn, 0) not executed immediately? Because it goes through the Event Loop. 👉 Call stack must be empty first. 💡 Tricky JS questions test understanding of how JavaScript really works. #Trick #js #javascript #Interview #follow #@all #Tips
To view or add a comment, sign in
-
🔥 𝗦𝘁𝗿𝘂𝗴𝗴𝗹𝗲𝗱 𝗪𝗶𝘁𝗵 𝗝𝗦 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀? ⤵️ String Polyfills and Common Interview Methods in JavaScript ⚡ 🔗 𝗥𝗲𝗮𝗱 𝗵𝗲𝗿𝗲: https://lnkd.in/dzE2rHMF 𝗧𝗼𝗽𝗶𝗰𝘀 𝗰𝗼𝘃𝗲𝗿𝗲𝗱 ✍🏻: ⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ ⇢ What string methods really are (String.prototype deep dive) ⇢ What polyfills are & why they matter in real-world apps ⇢ The common mistake developers make while writing polyfills ⇢ How interview questions actually test your thinking ⇢ Implementing core methods: trim, includes, startsWith, repeat ⇢ replaceAll explained (with tricky edge cases) ⇢ Classic interview problems: reverse string & count occurrences ⇢ Unicode & edge cases most developers miss ⇢ Tradeoffs: performance vs compatibility ⇢ How Babel + core-js handle polyfills in production Thanks Hitesh Choudhary Sir, Piyush Garg Sir, and the amazing Chai Aur Code community 🙌 #ChaiAurCode #JavaScript #InterviewPrep #WebDevelopment #Programming #SystemDesign #Hashnode
To view or add a comment, sign in
-
20 JavaScript questions that help you to crack frontend interview 1. What are higher-order functions in JavaScript, and can you provide an example? 2. What is destructuring in JavaScript, and how is it useful? 3. What are template literals in JavaScript, and how do they work? 4. How does the spread operator work in JavaScript? 5. What is the rest parameter in JavaScript, and how does it differ from the arguments object? 6. What is the difference between an object and an array in JavaScript? 7. How do you clone an object or array in JavaScript? 8. What are object methods like Object.keys(), Object.values(), and Object.entries()? 9. How does the map() method work in JavaScript, and when would you use it? 10. What is the difference between map() and forEach() in JavaScript? 11. What is event delegation in JavaScript, and why is it useful? 12. What are JavaScript modules, and how do you import/export them? 13. What is the prototype chain in JavaScript, and how does inheritance work? 14. What is bind(), call(), and apply() in JavaScript, and when do you use them? 15. How does JavaScript handle equality comparisons with == and ===? 16. What is the Document Object Model (DOM), and how does JavaScript interact with it? 17. How do you prevent default actions and stop event propagation in JavaScript? 18. What is the difference between synchronous and asynchronous code in JavaScript? 19. What is the difference between an event object and a custom event in JavaScript 20. How do you optimize performance in JavaScript applications? #frontend #reactjs #nextjs #javscript #community #interview
To view or add a comment, sign in
-
Important JavaScript Concepts Every Developer Must Master JavaScript is not just about syntax or frameworks it’s about understanding how the language behaves at runtime. These notes focus on the most important JavaScript concepts that directly impact real-world applications, performance, and interview outcomes. Instead of surface-level explanations, this collection breaks down execution flow, memory behavior, and async handling, helping developers move from trial-and-error coding to predictable, confident development. These concepts form the foundation for frameworks like React, Angular, and Node.js, and mastering them makes learning any new library significantly easier. Key Concepts Covered Core JavaScript Fundamentals Execution Context & Call Stack Scope, Lexical Environment & Scope Chain Hoisting (var, let, const) Value vs Reference Functions & Objects this keyword (implicit, explicit, arrow) Closures & memory behavior Higher-Order Functions Prototypes & Inheritance Asynchronous JavaScript Callbacks & callback hell Promises & microtask queue Async/Await execution flow Event Loop (microtasks vs macrotasks) Advanced & Interview-Critical Topics Debouncing & Throttling Currying & Function Composition Shallow vs Deep Copy Equality (== vs ===) Polyfills & custom implementations Performance & Best Practices Memory leaks & garbage collection basics Immutability & state updates Optimizing loops & async operations Writing predictable, clean JS Why These Concepts Matter Frequently asked in frontend & full-stack interviews Essential for writing efficient React code Help debug complex async bugs faster Build strong fundamentals for system design Who Should Learn This Frontend developers Full-stack engineers React / Angular developers Anyone preparing for JavaScript interviews #Frontend #WebDevelopment #JavaScriptInterview #ReactJS #NodeJS
To view or add a comment, sign in
-
🚨 JavaScript Async Interview Question What will be the output? function debounce(fn, delay) { let timer; return function (value) { clearTimeout(timer); timer = setTimeout(() => { fn(value); }, delay); }; } const log = debounce((v) => console.log(v), 100); log("A"); setTimeout(() => log("B"), 50); setTimeout(() => log("C"), 120); setTimeout(() => log("D"), 180); This question tests your understanding of: • Debounce behavior • setTimeout scheduling • How timers get cancelled and replaced • JavaScript async execution Many developers assume all values will print, but debounce changes the execution flow. What do you think the output will be? #JavaScript #FrontendInterview #Debounce #AsyncJavaScript #ReactJS #ProductBasedCompany
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development