Skip to content
YouTurkey Tech
All JavaScript topics

Primitive vs Reference Types

Beginner

Why copying a number gives you a separate value, but copying an object gives you a second name for the same object.Builds on Execution Context

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/18Press Play, or step through with the arrows, to begin.
Step 0 of 18

Explanation

2 min read

On this page

What are primitive and reference types in JavaScript?

JavaScript has seven types: string, number, bigint, boolean, undefined, null and symbol. A primitive is a single, immutable value. Everything else is an object: plain objects, arrays, functions, dates, maps. Objects are reference types: a variable never holds the object itself, only a reference to it.

How are primitives and objects stored and copied?

The usual mental model is the stack and the heap:

  • A primitive sits right in the variable's slot. let b = a copies the value, and the two variables are independent from then on.
  • An object lives in the heap. The variable holds a reference to it, like an address. const admin = user copies the reference, so both names reach the same object.
  • Calling a function copies each argument into its parameter, the same way. For an object, the parameter gets a copy of the reference: the function can change the object, but reassigning the parameter only moves the local name.

Engines are free to store things elsewhere (long strings live in the heap, for example), but your code always behaves as this model describes.

What mistakes do people make with primitives and references?

Mini example

function addItem(cart, item) {  return [...cart, item]; // a new array, the caller's cart is untouched} const cart = ['apple'];const next = addItem(cart, 'pear'); console.log(cart); // ['apple']console.log(next); // ['apple', 'pear']console.log(cart === next); // false

Quick check

What does { x: 1 } === { x: 1 } evaluate to?

Further reading

All steps as text
  1. Step 1:

    The global context starts. Variables live in its scope, and objects will live in the heap.

  2. Step 2:

    Creation phase: a, b, user and admin are reserved, and rename is stored as a function.

    Lines 1–14

  3. Step 3:

    5 is a primitive, so it is stored right in the variable a.

    Line 1

  4. Step 4:

    b = a copies the value. b gets its own 5, with no link back to a.

    Line 2

  5. Step 5:

    b = 10 replaces the value in b only.

    Line 3

  6. Step 6:

    It prints 5 10: changing b never touched a.

    Line 4

  7. Step 7:

    The object is created in the heap. user does not hold the object, only a reference to it.

    Line 6

  8. Step 8:

    admin = user copies the reference, not the object. Both names now point to the same object.

    Line 7

  9. Step 9:

    admin.name = 'Sara' follows the reference and changes the one shared object.

    Line 8

  10. Step 10:

    user.name reads the same object, so it prints Sara.

    Line 9

  11. Step 11:

    rename(user) copies the reference into the parameter person. Now three names point to one object.

    Line 16

  12. Step 12:

    person.name = 'Omar' follows the reference, so it changes the caller's object too.

    Line 12

  13. Step 13:

    Assigning to person points the local name at a new object. user is untouched: only the local reference moved.

    Line 13

  14. Step 14:

    rename returns and its scope is discarded. Nothing points to Object @2 any more, so it is garbage collected.

    Line 14

  15. Step 15:

    It prints Omar: the change through the reference stuck, the reassignment did not.

    Line 17

  16. Step 16:

    === on objects compares references. user and admin point to the same object, so true.

    Line 18

  17. Step 17:

    Each { x: 1 } literal creates a new object. Same contents, two references, so false.

    Line 18

  18. Step 18:

    Output: 5 10, Sara, Omar, true false. Primitives are copied, objects are shared through references.