الانتقال إلى المحتوى
FrontendJavaScript· 6 دقائق للقراءة

JavaScript Doesn't Pass Objects by Reference: Pass by Sharing Explained

A function gets a copy of the reference: it can change your object, but never your variable.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • ES2025
محتويات المقال

A product list has a "Clear filters" button. Its handler calls clearFilters(filters), which sets the parameter to a fresh copy of the defaults. Nothing happens: no error, and the old filters stay applied. A few files away, a helper that "only reads" the search query adds a default sort to it, and the URL suddenly gains a sort order the user never picked. Both bugs come from the same sentence the team believes: "objects are passed by reference".

The common misconception

The belief: primitives are passed by value and objects by reference, so a function that receives an object has full access to the caller's variable, including replacing it with a different object.

Half of it is easy to observe. Change an object's property inside a function and the caller sees the change, which is exactly what "by reference" seems to promise. Tutorials and interview answers repeat the phrase, and "reference" is the right word for what a variable holds when its value is an object. The mistake is in "pass by": what the function receives is a copy of that reference, not access to the caller's variable.

What actually happens

A variable holding an object holds a reference

When you write let filters = { category: "phones" }, the object lives somewhere in memory and filters holds a reference to it. const saved = filters copies the reference, not the object, so you get two variables and one object. Nothing about this is specific to functions yet. Primitives follow the same copy rule; you just can't tell, because a number or a string can't be changed in place, so a copy and a shared value behave the same.

A call copies each argument into a new variable

MDN puts it in one line: arguments are always passed by value and never by reference, and object arguments are passed by sharing. Each parameter is a new local variable, initialized with a copy of the argument's value. For an object, that copy is the same reference, so the function and the caller now share one object. That's : the object is shared, the variables are not.

Mutation goes through, reassignment doesn't

Two lines that look alike do very different things. current.category = "all" follows the reference and changes the shared object, so the caller sees it. current = { ...DEFAULTS } points the local variable at a new object, and the caller's variable still points at the old one. In a language with real pass by reference, like a C# ref parameter or a C++ reference parameter, the parameter is an alias for the caller's variable, so that reassignment would reach the caller. JavaScript has no equivalent.

JavaScript filters.js
const DEFAULTS = { category: "all", sort: "newest" };let filters = { category: "phones", sort: "price" };function clearFilters(current) {  current = { ...DEFAULTS }; // rebinds the parameter, nothing else}clearFilters(filters);console.log(filters.category); // phones: the caller's variable is untouched

Returning the new value puts the decision where it belongs: only the code that owns filters can point it at another object. If a function really must update the caller's object in place, it has to mutate it, for example with Object.assign(current, DEFAULTS), and its name should say so.

The other half: accidental mutation

The same sharing that made the reset fail makes the sort bug possible. withDefaultSort below looks like it returns an improved query, but it changes the caller's object on the way:

JavaScript query.js
function withDefaultSort(query) {  query.sort ??= "newest"; // mutates the object the caller passed  return query;}const query = { q: "headphones", price: { max: 500 } };const sorted = withDefaultSort(query);console.log(query.sort); // newest: the caller's object changedconsole.log(sorted === query); // true: one object, two namesconst copy = { ...query };copy.q = "speakers"; // top level was copiedcopy.price.max = 900; // nested object is still sharedconsole.log(query.q, query.price.max); // headphones 900

const didn't protect query. MDN describes const as an immutable binding, not an immutable value: the variable can't be reassigned, but the object's properties can still change. And spread makes a : top-level properties are copied, while nested objects like price are still shared, which is why copy.price.max = 900 changed the original too.

What the function does with its parameterDoes the caller see it?
param.prop = value or param.push(item)Yes: it changes the shared object
Object.assign(param, changes)Yes: it copies into the shared object
param = newObjectNo: only the local variable moves
param = { ...param, prop: value }No: a new object, held by the local variable
count += 1 on a number parameterNo: the number was copied into a local variable

How to apply it in your project

  1. Never reassign a parameter to "return" a valueIf a function computes a new object, return it and let the caller assign it. Reassigning a parameter only ever changes the function's own variable.
  2. Don't mutate what you were givenBuild a new object instead, like return { ...query, sort: query.sort ?? "newest" }. If in-place mutation is the point, name the function after it.
  3. Copy as deep as you changeSpread one level for flat objects, spread each nested level you modify, and use structuredClone for whole trees of plain data.
  4. Use const and freeze for what they really doconst stops reassignment of a variable. Object.freeze stops changes to an object, but only one level deep, so it's best as a development guard on shared configuration.

Key takeaways

  • JavaScript passes every argument by value; for objects, the value that gets copied is a reference.
  • The function and the caller share the object, so mutating its properties is visible outside.
  • Reassigning a parameter only changes the function's local variable; the caller's variable never moves.
  • const and spread don't make objects immutable: const blocks reassignment, and spread copies one level.
  • To replace what a caller's variable points to, return a new value and let the caller assign it.

📚 المصادر

  1. Functions: Passing arguments — MDN
  2. const — MDN
  3. Shallow copy — MDN Glossary
  4. Window: structuredClone() method — MDN
  5. The structured clone algorithm — MDN

التعليقات (0)