Is JavaScript pass by value or pass by reference?

BeginnerJavaScript interview questionBy , updated 17 November 2026

Short answer

JavaScript is always pass by value. For a primitive, the value is the number, string or boolean itself. For an object or array, the value that gets copied is a reference to the object, not the object. So a function can change the object's contents and the caller sees it, but reassigning the parameter never affects the caller's variable. This is often called "call by sharing".

The precise answer

Picture a variable that holds an object as holding an address. When you pass it to a function, the address is copied into the parameter. Now two variables point at the same object.

  • Mutating through the parameter (user.name = "Ravi", list.push(4)) changes the shared object, so the caller sees it.
  • Reassigning the parameter (user = { name: "Ravi" }) only changes where the local copy points. The caller's variable still holds the old address.

That second point is the test. In a language with real pass by reference, such as C++ with & parameters or C# with ref, assigning to the parameter would replace the caller's variable. In JavaScript it never does.

Primitives: a copy of the value

function addOne(n) {
  n = n + 1;
  return n;
}

let count = 5;
addOne(count);      // 6
console.log(count); // 5: the function changed its own copy

Primitives are also immutable, so there is nothing to mutate: the parameter gives the function no way to reach the caller's count. Methods such as toUpperCase() on strings return new strings.

Objects: a copy of the reference

function rename(user) {
  user.name = "Ravi";        // mutation: changes the shared object
}

function replace(user) {
  user = { name: "Ravi" };   // reassignment: changes only the local variable
}

const person = { name: "Asha" };

replace(person);
console.log(person.name); // "Asha"

rename(person);
console.log(person.name); // "Ravi"

Both functions receive the same thing: a copy of the reference to person. The difference is what they do with it.

The same applies to arrays:

function clear(list) {
  list.length = 0;   // empties the caller's array
}

function reset(list) {
  list = [];         // does nothing outside
}

The swap test

A classic way to prove there is no pass by reference:

function swap(a, b) {
  const temp = a;
  a = b;
  b = temp;
}

let x = { id: 1 };
let y = { id: 2 };
swap(x, y);
console.log(x.id, y.id); // 1 2: unchanged

With true pass by reference, x and y would have swapped. Here only the local a and b swapped.

Assignment works the same way

Passing an argument behaves like assigning it to a new variable. const copy = original copies the reference, so both names point at one object:

const original = { tags: ["js"] };
const copy = original;
copy.tags.push("css");
console.log(original.tags); // ["js", "css"]
console.log(copy === original); // true: same object

This is also why === on objects compares identity, not contents. Two objects with the same properties are not equal unless they are the same object.

const does not help here. It stops you reassigning the variable, but the object it points at can still be mutated. To get a separate object, make a copy with spread or structuredClone. The shallow vs deep copy answer explains which copy you need.

Why it matters in real code

  • Functions that mutate their inputs surprise callers. A sortUsers(users) that calls users.sort() reorders the caller's array as a side effect. Prefer returning a new value, such as users.toSorted(...). The case for this is in pure functions and immutability.
  • React and Redux detect changes by comparing references. If you mutate state and pass the same object back, the reference has not changed and the update can be skipped. Create a new object instead: setUser({ ...user, name: "Ravi" }).
  • Shared defaults. A module-level object passed into many functions can be changed by any of them. Freezing it or copying it on the way in avoids that.

Practise it

Predict pass-by-value and pass-by-sharing results tests exactly the reassignment versus mutation split. Predict shallow versus deep copy results shows which copies share nested objects, and Implement an immutability helper and Apply a JSON Patch without mutating the document make you update data without touching the original.

Common follow-up questions

So are objects passed by reference?

Not in the strict sense. The reference is passed by value. People say "objects are passed by reference" as shorthand, and in an interview it is worth giving the precise version: you can mutate the object, but you cannot rebind the caller's variable.

How do you stop a function from changing your object?

Pass a copy (fn({ ...obj }) or fn(structuredClone(obj))), or freeze it with Object.freeze(obj). Freezing is shallow, so nested objects can still change unless you freeze them too.

Are strings passed by reference because they are large?

Engines may share the underlying memory, but you cannot observe that. Strings are immutable primitives, so they behave exactly as if copied.

What about returning an object from a function?

Return values follow the same rule. The caller gets a reference to the object, so if the function also stored it somewhere, both places share it. A cache that returns its internal array lets callers mutate the cache.

Mistakes to avoid

  • Saying JavaScript "passes objects by reference and primitives by value" without explaining that reassignment does not reach the caller

  • Assuming const makes an object immutable

  • Writing helpers that sort, splice or edit their arguments in place without saying so

  • Expecting obj1 === obj2 to compare contents

  • Assuming a spread copy is independent all the way down

    It only copies the top level.

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Predict pass-by-value and pass-by-sharing results: reassigned parameters versus mutated objectsEasy15 min
  2. Predict shallow versus deep copy results: spread, Object.assign, JSON and structuredCloneEasy20 min
  3. Implement an immutability helper with $set, $push, $merge and $applyMedium40 min
  4. Apply a JSON Patch (RFC 6902) without mutating the documentMedium40 min