JavaScript: Pass by Value vs Pass by Reference
Clearing the Confusion Around JavaScript Function Arguments
A software engineer who likes to explore new technologies, problem-solving, build projects, and have a keen interest in Web development.
Search for a command to run...
Clearing the Confusion Around JavaScript Function Arguments
A software engineer who likes to explore new technologies, problem-solving, build projects, and have a keen interest in Web development.
No comments yet. Be the first to comment.
How a 3-line Prisma query fired 1,847 database queries per request — and how we found, fixed, and prevented it from ever coming back.

When building backend systems, especially SaaS applications, handling background jobs correctly is critical. Whether it's sending emails, processing payments, or scheduling reminders, choosing the rig
Guide to Zero-Interruptions Deployment for Node.js on AWS EC2

When we work with objects and arrays in JavaScript, we often need to copy data. But copying in JavaScript is not always as simple as it looks. Depending on the method you use, you may end up with shared references which can cause unexpected changes i...

When working with functions in JavaScript, you’ll often hear developers debating whether JavaScript is “pass by value” or “pass by reference.” The truth is slightly nuanced. Let’s break it down with examples so you’ll never get confused again.
When you pass a primitive type (like number, string, boolean, null, undefined, symbol, bigint) into a function, JavaScript passes it by value. This means the function gets a copy, and changes inside the function don’t affect the original variable.
function updateValue(x) {
x = x + 10;
console.log("Inside function:", x);
}
let num = 5;
updateValue(num);
console.log("Outside function:", num); // Still 5
Here, the variable num is unaffected because only a copy was modified.
For objects and arrays, JavaScript passes a reference — but not the actual object itself. Instead, it passes a copy of the reference. That’s why changes to the object inside a function reflect outside.
function updateSkills(obj) {
obj.skills.push("Node.js");
}
let user = { name: "Pratik", skills: ["JavaScript"] };
updateSkills(user);
console.log(user.skills); // ["JavaScript", "Node.js"]
Here, user was updated because the function received a reference to the same memory location.
Primitives → passed by value
Objects/arrays → passed by reference copy (so modifications affect the original)
JavaScript is always pass by value — but when dealing with objects, the value being passed is actually a reference to the object. Understanding this subtlety helps avoid confusion in interviews and in real-world debugging.