Primitive vs Reference Types
BeginnerWhy 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
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
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 = acopies 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 = usercopies 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); // falseQuick check
What does { x: 1 } === { x: 1 } evaluate to?
Further reading
All steps as text
- Step 1:
The global context starts. Variables live in its scope, and objects will live in the heap.
- Step 2:
Creation phase:
a,b,userandadminare reserved, andrenameis stored as a function.Lines 1–14
- Step 3:
5 is a primitive, so it is stored right in the variable
a.Line 1
- Step 4:
b = acopies the value.bgets its own 5, with no link back toa.Line 2
- Step 5:
b = 10replaces the value inbonly.Line 3
- Step 6:
It prints 5 10: changing
bnever toucheda.Line 4
- Step 7:
The object is created in the heap.
userdoes not hold the object, only a reference to it.Line 6
- Step 8:
admin = usercopies the reference, not the object. Both names now point to the same object.Line 7
- Step 9:
admin.name = 'Sara'follows the reference and changes the one shared object.Line 8
- Step 10:
user.namereads the same object, so it prints Sara.Line 9
- Step 11:
rename(user)copies the reference into the parameterperson. Now three names point to one object.Line 16
- Step 12:
person.name = 'Omar'follows the reference, so it changes the caller's object too.Line 12
- Step 13:
Assigning to
personpoints the local name at a new object.useris untouched: only the local reference moved.Line 13
- Step 14:
rename returns and its scope is discarded. Nothing points to Object @2 any more, so it is garbage collected.
Line 14
- Step 15:
It prints Omar: the change through the reference stuck, the reassignment did not.
Line 17
- Step 16:
===on objects compares references.userandadminpoint to the same object, so true.Line 18
- Step 17:
Each
{ x: 1 }literal creates a new object. Same contents, two references, so false.Line 18
- Step 18:
Output: 5 10, Sara, Omar, true false. Primitives are copied, objects are shared through references.