Object identity and shared mutation
Quick answer
Two bindings can hold the same object value. Updating a property through one binding changes the object observed through the other. Separate literals create separate objects.
How it works
Assignment of an object value does not clone it. Strict equality compares identity. A shallow copy creates a new outer object while nested object values may still be shared. Object.assign({}, source) is one shallow-copy technique; it copies enumerable own fields.
const a = { stock: 3 };const b = a;b.stock = 8;console.log(a.stock, a === b);Expected output in Node.js:
8 trueBoth bindings share a single object.
const a = { stock: 3 };const b = Object.assign({}, a);b.stock = 8;console.log(a.stock, a === b);Expected output in Node.js:
3 falseA new outer object receives the same primitive field value.
Common mistakes
Do not infer deep copying from distinct outer identities. Copy depth is part of the contract. Object.assign can invoke getters/setters; use simple trusted records here.
Practice
Compare aliasing and a shallow copy of a product with a primitive stock field.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Object identity and shared mutation — Knowledge Check
Apply the taught concept and reason about the result.
What does const a={n:1}; const b=a; b.n=7; a.n produce?
What does ({n:1}) === ({n:1}) return?
What does Object.assign({}, source) generally create for ordinary records?
When a shallow copy contains a nested object, what is true of that nested value?
Does a const binding prevent mutation of a writable object field?