References and sharing: one object, two names

In JavaScript an object is not stored inside the binding that names it. The object lives on the heap, and the binding holds a reference to it. A second binding assigned from the first copies that reference, not the object, so two names point at one value. Numbers, strings, booleans, null, undefined, symbol and bigint are primitives, and those really are copied.

The difference decides what a program sees. A change made through one name appears through the other when the value is an object or an array, === compares references rather than contents, and a function that receives an object can change the object the caller passed. This lesson shows the rule with four short programs and one program that replaces a parameter, which is where the rule stops.

A binding holds a reference

const user = { name: 'Ada' }; creates one object and binds the name user to it. const alias = user; copies the reference, so the object now has two names. Neither name owns the object: the runtime frees it when the last reference is gone, and until then every name sees the same properties.

const user = { name: 'Ada' }; const alias = user; alias.name = 'Grace'; console.log('user.name:', user.name); console.log('alias.name:', alias.name); console.log('same object:', user === alias); const numbers = [1, 2, 3]; const sameNumbers = numbers; sameNumbers.push(4); console.log('numbers:', numbers.join(', ')); console.log('same array:', numbers === sameNumbers);

node refs.mjs

user.name: Grace
alias.name: Grace
same object: true
numbers: 1, 2, 3, 4
same array: true

Nothing was copied. The write on the fourth line went into the one object, and both names print the new name because both point at it. The array behaves in the same way: push changed the single array, and the length the original name reports grew with it.

Primitives and objects

A number is a primitive, and assignment copies it. After let b = a; the two bindings hold two values that happen to be equal, so assigning 20 to b leaves a alone. The same statement with an object on the right does something else entirely, and the type of the value is what decides which one you get.

let a = 10; let b = a; console.log('before:', a, b); b = 20; console.log('a:', a); console.log('b:', b); const point = { x: 1, y: 2 }; const moved = point; moved.x = 100; console.log('point.x:', point.x); console.log('moved.x:', moved.x); console.log('same point:', point === moved);

node primitives.mjs

before: 10 10
a: 10
b: 20
point.x: 100
moved.x: 100
same point: true

The first three lines are the primitive story: one value was copied and the copy moved on its own. The last three are the object story: no copy was made, so the write through moved is visible through point. To get a real copy of an object, the next lesson uses the spread syntax, written { ...point }.

Identity and ===

For objects and arrays, === asks whether both sides are the same value, and for a reference that means the same object. Two objects with equal contents are still two objects, so the comparison is false. Numbers and strings are compared by value, which is why the ids in the program below are equal while the objects are not.

const firstEmpty = {}; const secondEmpty = {}; const firstList = []; const secondList = []; console.log('two objects:', firstEmpty === secondEmpty); console.log('two arrays:', firstList === secondList); const user = { id: 1 }; const alias = user; const copy = { id: 1 }; console.log('alias:', user === alias); console.log('copy:', user === copy); console.log('same id:', user.id === copy.id);

node identity.mjs

two objects: false
two arrays: false
alias: true
copy: false
same id: true

This is the reason a lookup table needs a Map or a key rather than indexOf on a list of objects: the question "is this the object I stored" is answered by identity, and a new object with the same fields is a different entry. Object.is works the same way for objects and differs from === only on NaN and on the sign of zero, which the lesson on operators covers.

Passing an object to a function

An argument is assigned to a parameter, and that assignment copies whatever the value is. For an object the copy is the reference, so the function and the caller hold two names for one object. A function can therefore change the object it was given, and it still cannot change which object the binding of the caller points at.

function rename(person, name) { person.name = name; return person; } function addItem(list, item) { list.push(item); } const user = { name: 'Ada' }; const numbers = [1, 2]; const returned = rename(user, 'Grace'); addItem(numbers, 3); console.log('user.name:', user.name); console.log('returned is user:', returned === user); console.log('numbers:', numbers.join(', '));

node arguments.mjs

user.name: Grace
returned is user: true
numbers: 1, 2, 3

rename returned the object it was handed, which is why returned and user are the same value. A function that has to leave that object alone has two options: change a copy, which the next lesson builds, or return a new object instead of changing the old one.

Exercises

  1. Add a third name for the object in refs.mjs and change its name through that name. Which lines of the output change?
  2. Replace const moved = point; in primitives.mjs with const moved = { ...point }; and run it again. What does point.x print now, and what happens to the last line?
  3. In identity.mjs, print firstList === firstList as well. What is the answer, and what does the comparison ask for when both sides are the same name?
  4. Write a function that sets person = { name: 'Grace' }; instead of person.name = 'Grace'; and call it with the user object. What does user.name print afterwards, and why?
  5. Change addItem so that it returns [...list, item] instead of pushing. What does the numbers line print then?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a binding hold after const user = { name: "Ada" };?

    Answer

    A reference to the object, which lives elsewhere

    The object lives on the heap, and the binding holds a reference to it; primitives are the values that are stored directly.

  2. What does this program print? const user = { name: "Ada" }; const alias = user; alias.name = "Grace"; console.log(user.name);

    Answer

    Grace

    The assignment copied the reference, so both names point at one object and the write through the alias is visible through the original.

  3. What does this program print? const user = { name: "Ada" }; const alias = user; console.log(user === alias);

    Answer

    true

    Both bindings hold the same reference, and === for objects asks whether both sides are the same object.

  4. What does ({ id: 1 }) === ({ id: 1 }) evaluate to?

    Answer

    false, because the two literals create two objects

    Identity compares references, and two literals are two different objects however equal their fields are.

  5. What does this program print? let a = 1; let b = a; b = 2; console.log(a);

    Answer

    1

    A number is a primitive, so the assignment copied the value and the second binding moved on alone.

  6. What does this program print? const point = { x: 1 }; const moved = point; moved.x = 100; console.log(point.x);

    Answer

    100

    No copy was made: moved holds the same reference, so the write is visible through point.

  7. A function receives an array and calls list.push(4) on it. What does the caller see?

    Answer

    The caller array gained the element as well

    The parameter holds a copy of the reference, so the push acts on the one array the caller also names.

  8. What does this program print? function rename(person) { person = { name: "Grace" }; } const user = { name: "Ada" }; rename(user); console.log(user.name);

    Answer

    Ada

    Assigning a new object to the parameter only repoints the parameter; the caller binding still holds the old object.

  9. Which values are copied whole on assignment?

    Answer

    Numbers, strings, booleans, null, undefined, symbols and bigints

    Those are the primitives, and they are the values that behave like a copy; objects and arrays share their reference.

  10. Why does a second name for an array change the first one when it pushes?

    Answer

    Because an array is always copied by reference and both names point at one array

    The push changes the single array that both bindings reference, so the length and the elements are seen everywhere.

The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev