Avoiding State
Mutation
Learn why mutating shared state leads to bugs, and how to write code with clear, predictable data flow instead.
What Is State?
State is any data your program remembers over time: a user's name, a shopping cart, a counter value, or even a configuration object. Whenever your code reads or writes to a variable that holds information, it is interacting with state.
What Is Mutation?
Mutation means changing an existing value in place. In JavaScript, objects and arrays are stored by reference: when two variables point to the same object, changing it through one variable affects the other.
Why Does This Matter?
Unintended mutation is one of the most common sources of bugs in JavaScript applications. When shared state changes unexpectedly, you get:
The Core Rule
Clear Data Flow
Instead of many functions reaching into the same mutable object, design your code so that data flows in one direction: each function takes input, returns new output, and never touches the original.
Mutable vs Immutable Patterns
| Mutable | Immutable | |
|---|---|---|
| Add to array | arr.push(item) | [...arr, item] |
| Remove from array | arr.splice(i, 1) | arr.filter(x => x !== val) |
| Update object field | obj.key = val | {...obj, key: val} |
| Sort an array | arr.sort() | [...arr].sort() |
| Predictability | Low | High |
How to Apply This
Example 1: Arrays
A function needs to add a new item to a list. Compare the mutating approach with the immutable approach.
Example 2: Objects
A function updates a user's email. The mutating version overwrites the field directly. The immutable version returns a fresh object.
Example 3: Nested Objects
Nested data requires spreading at every level you change. A shallow spread only copies the top layer: inner objects are still the same references.
Object.freeze() as a Safety Net
Object.freeze() makes an object immutable at the top level. In strict mode, any attempt to change a frozen property throws a TypeError.
The Big Picture
Common Mistakes
Summary Table
| Concept | Key Idea |
|---|---|
| State | Data your program remembers over time |
| Mutation | Changing a value in place, by reference |
| Shared State Risk | One change can break distant code |
| Immutable Pattern | Copy first, then modify the copy |
| Spread (...) | Shallow clone of objects and arrays |
| Object.freeze() | Prevents top-level property changes |
| Nested Updates | Spread at every changed level |
Question 1 of 5
What does it mean to mutate a value in JavaScript?
Question 2 of 5
Code:
const original = [1, 2, 3];
const copy = original;
copy.push(4);
console.log(original.length);
What does console.log(original.length) print?
Question 3 of 5
Which of the following creates a new array instead of mutating the original?
Question 4 of 5
What is the main limitation of Object.freeze()?
Question 5 of 5
Code:
const state = { user: { name: "Bob", age: 25 } };
const next = { ...state, user: { ...state.user, age: 26 } };
After this code runs, what is the value of state.user.age?