Immutability
In one line
Immutability means an object's state cannot be modified after it is created; changes produce a new object instead.
In simple words
Immutability is a design principle where data, once created, cannot be changed. Instead of modifying an existing object, you create a new object with the desired changes. The original stays untouched. This sounds wasteful but enables powerful guarantees: you can share immutable objects freely without fear someone else will mutate them.
Immutable data makes programs easier to reason about. There are no surprise mutations from other parts of the code, so functions are more predictable and debugging is simpler. It also enables structural sharing — a new list can reuse most of the old list's nodes, so "copying" is cheap. React, Redux, and functional programming lean heavily on immutability.
In JavaScript, const prevents reassignment but not mutation of object contents. True immutability requires discipline (never mutating) or libraries like Immutable.js, or freezing objects with Object.freeze(). In functional languages like Haskell and Rust, immutability is the default and mutation is opt-in.
Example
// Immutable update — return a new object instead of mutating
function updateUser(user, newName) {
return { ...user, name: newName }; // new object, original untouched
}
const original = { name: "Ada", age: 36 };
const updated = updateUser(original, "Grace");
console.log(original.name); // "Ada" — unchanged
console.log(updated.name); // "Grace" — new object
console.log(original === updated); // false — different referencesupdateUser spreads the original into a new object and overrides name. The original object is never mutated.
Related terms
Related Resources
Related Lessons
- Functional Programming
Learn immutability and pure functions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.