Immutable Operations on Primitives
You'll learn: Why JavaScript primitives are inherently immutable and how every operation on them creates new values rather than modifying the original.
The Core Idea
In JavaScript, primitives (numbers, strings, booleans, null, undefined, symbol, bigint) are immutable by design. This means you cannot change their actual value—you can only create new ones. Every operation on a primitive produces a brand-new value while leaving the original untouched.
Why This Matters
Think of primitives like carved stone tablets: once created, their content cannot be altered. If you want different content, you must create a new tablet. This is fundamentally different from objects and arrays, which are like whiteboards—you can erase and rewrite parts without creating a new board.
let x = 5;
let y = x;
y = y + 10;
console.log(x); // 5 (unchanged)
console.log(y); // 15 (new value)
Even though y started as a reference to x's value, adding 10 created a completely new number. The original 5 remains unchanged.
String Operations Are Always Immutable
let greeting = "Hello";
let upper = greeting.toUpperCase();
console.log(greeting); // "Hello" (original unchanged)
console.log(upper); // "HELLO" (new string)
The toUpperCase() method doesn't modify greeting—it returns a brand-new string. The original string is frozen in time.
The Natural Advantage
Because primitives are already immutable, you don't need to worry about accidentally mutating them when passing them to functions or using them in pure functions. They naturally support functional programming principles without extra effort.
Key Takeaway: Primitives in JavaScript are immutable by nature—every operation creates a new value, making them inherently safe and predictable for functional programming patterns.