Pass By Reference
A variable that holds an object does not hold the object itself, but a reference to it, which is a pointer to where the object lives in memory. Assigning that variable to another one, or passing it to a function, copies the reference, so both names then point at the same object. Primitive values such as numbers and strings have no such sharing, because they cannot be changed in place.
Strictly speaking, JavaScript always passes arguments by value. For an object, the value that gets copied is the reference, which is sometimes called call by sharing. The practical effect is described below.
Primitives are copied
Section titled “Primitives are copied”Assigning a primitive to a new variable gives the new variable its own value. Reassigning one of the two variables leaves the other unchanged.
let original = 'draft';let copy = original;copy = 'final';
console.log(original, copy);This prints:
draft finalObjects are shared
Section titled “Objects are shared”Assigning an array or object copies only the reference. A change made through either name, such as push on an array or setting a property, is a change to the one shared object.
const basket = ['bread', 'milk'];const sameBasket = basket;
sameBasket.push('eggs');
console.log(basket);console.log(basket === sameBasket);This prints:
[ 'bread', 'milk', 'eggs' ]trueThe same applies to equality, which compares references rather than contents. Two objects with identical properties are not equal unless they are the same object.
const a = { size: 'M' };const b = { size: 'M' };const c = a;
console.log(a === b);console.log(a === c);This prints:
falsetrueFunctions can change the caller’s object
Section titled “Functions can change the caller’s object”A function that receives an object can change the caller’s object, because its parameter refers to the same object.
function addTax(order) { order.total = order.total * 1.2;}
const order = { id: 7, total: 50 };addTax(order);console.log(order);This prints:
{ id: 7, total: 60 }Reassigning a parameter does not
Section titled “Reassigning a parameter does not”Assigning a new object to the parameter only points the parameter somewhere else. The caller’s variable still refers to the original object, which is the difference between JavaScript and a language with true pass-by-reference.
function replaceOrder(order) { order = { id: 99, total: 0 }; return order;}
const order = { id: 7, total: 50 };const result = replaceOrder(order);console.log(order);console.log(result);This prints:
{ id: 7, total: 50 }{ id: 99, total: 0 }Avoiding surprises
Section titled “Avoiding surprises”A function that changes its arguments is hard to reason about, since a caller may not expect its data to change. A common pattern is to return a new object and leave the argument alone. The spread syntax makes the copy.
function withTax(order) { return { ...order, total: order.total * 1.2 };}
const order = { id: 7, total: 50 };const taxed = withTax(order);console.log(order);console.log(taxed);This prints:
{ id: 7, total: 50 }{ id: 7, total: 60 }The spread syntax copies only the top level, so any nested arrays or objects are still shared between the two. Deep vs Shallow Copy explains the difference and how to copy every level.
const does not help here, because it stops the variable being reassigned but does not stop the object it refers to from being changed. To make an object read-only, see Seal, Freeze & Prevent Extensions.
Steve Griffith covers this topic in his video What Does Pass By Reference Mean in JS.