Learn JavaScript
❯ types.primitives

Primitives vs Objects in JavaScript

$concept

JavaScript has seven primitives — string, number, bigint, boolean, undefined, symbol, null — copied by value and compared by value, plus objects (including arrays and functions) copied and compared by reference. This single divide explains 2 + 2 equaling 4 while [2] + [2] gives the string "22", and why const arr = [] still allows arr.push().

Primitives are immutable (methods return new values; the original never changes), while objects mutate through any live reference. Primitives also auto-box temporarily when you call methods on them ("hi".length works because a wrapper object briefly exists), then discard the wrapper.

$how it works
1

Seven primitives

string, number, bigint, boolean, undefined, symbol, null — everything else (arrays, functions, dates) is an object.

2

Value vs reference

Primitives copy the value (a = b snapshots); objects copy the address (two names, one shared object).

3

Primitives never mutate

s.toUpperCase() returns a new string — the original is untouched, always, by design.

4

Auto-boxing on access

"hi".length briefly wraps the primitive to read a property, then discards the wrapper — convenient, invisible, occasionally surprising.

$example
let a = 5;
let b = a;
b = 6;
let arr1 = [1];
let arr2 = arr1;
arr2.push(2);
console.log(a, b, arr1.length);
Output
5 6 2
1a copies 5 into b by value — reassigning b leaves a at 5.
2arr2 references the same array as arr1 — pushing through either name grows the shared object to length 2.
35 6 2 prints — value independence for primitives, shared fate for objects.
$use cases
Predicting copy-vs-share behavior before choosing a data flow
Cloning objects deliberately (spread, structuredClone) instead of aliasing
Understanding why const arrays still mutate (binding fixed, contents not)
Reading [2] + [2] === "22" as reference-plus-coercion, not arithmetic

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.