intermediate · 20 minute study
Two names can point to one object
Unexpected shared mutation breaks UI state, caches, saves and tests even when every line looks simple.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Local storage — An existing guided foundation in Learn → Try → Test → Build.
First, build a mental model
A variable is a named binding to a value. When that value is an object, copying it copies a reference to the same object, not the whole object graph.
- Primitive values compare by value; objects compare by identity.
- Assignment of an object reference creates another path to the same object.
- Object spread copies immediate properties, not nested objects.
- Immutable updates must replace each modified level that needs a new identity.
const original = { name: 'Mira', stats: { score: 10 } };
const alias = original;
const shallow = { ...original };
shallow.stats.score = 99;
console.log(original.stats.score);
console.log(alias === original, shallow === original);
const next = { ...original, stats: { ...original.stats, score: 100 } };
console.log(next.stats === original.stats);- Alias
alias and original identify one object.
- Shallow copy
shallow is new, but shallow.stats still identifies original.stats.
- Nested update
next creates a new outer object and a new stats object.
Production thinking
Take this beyond the example.
State ownership and aliasing are architectural concerns. Freeze test fixtures where useful and test that pure functions leave inputs unchanged.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · A closure keeps access to its lexical environment →