Shallow vs Deep Copy
BeginnerWhy changing a copy made with spread can still change the original, and what structuredClone and JSON copy instead.Builds on Primitive vs Reference Types
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is the difference between a shallow and a deep copy?
A shallow copy creates a new top-level object, but its properties are copied as they are: a nested object is copied as a reference, so the original and the copy share it. A creates new objects at every level, so nothing is shared and changing the copy can never change the original.
How do spread, structuredClone and JSON copy an object?
- Shallow:
{ ...obj },Object.assign({}, obj),[...arr],arr.slice()andArray.from(arr). They are fast and fine for flat data. - Deep:
structuredClone(obj). It copies nested objects, arrays,Date,Map,Setand even circular references. It throws aDataCloneErroron functions and DOM nodes, and class instances come back as plain objects, without their methods. - Deep but lossy:
JSON.parse(JSON.stringify(obj)). It only keeps what JSON can express:- a
Datebecomes a string; undefined, functions and symbols are dropped (inside arrays they becomenull);NaNandInfinitybecomenull;MapandSetbecome{};- a circular reference or a
BigIntthrows aTypeError.
- a
What mistakes do people make when copying objects?
Mini example
const state = { user: { name: 'Ali', tags: ['admin'] }, theme: 'dark' }; // Copy only the path you change, and share the rest.const next = { ...state, user: { ...state.user, name: 'Sara' },}; console.log(state.user.name, next.user.name); // Ali Saraconsole.log(state.user.tags === next.user.tags); // trueQuick check
After const b = { ...a }; b.n = 2; b.inner.n = 2;, what is a.n, a.inner.n when a = { n: 1, inner: { n: 1 } }?
Further reading
All steps as text
- Step 1:
The global context starts in its creation phase.
- Step 2:
user,shallow,deep,eventandcopyare reserved but uninitialized, as everyconststarts.Lines 1–16
- Step 3:
A nested object is two objects in the heap.
userpoints to the outer one, and itsaddressholds a reference to the inner one.Lines 1–4
- Step 4:
Spread creates a new object but copies only the top level.
addressis copied as a reference, so both objects share the same address.Line 6
- Step 5:
shallow.name = 'Sara'changes the copy's own property.user.nameis still 'Ali'.Line 7
- Step 6:
shallow.addressleads to the shared inner object, so changing its city changes it forusertoo.Line 8
- Step 7:
It prints Ali Giza. The top level was copied, the nested level was shared: that is a shallow copy.
Line 9
- Step 8:
structuredClonecopies every level.deepgets a new outer object and its own address object.Line 11
- Step 9:
deep.address.city = 'Alex'changes only the clone's address.Line 12
- Step 10:
It prints Giza Alex: the original is untouched.
Line 13
- Step 11:
eventholds a Date object and a property set to undefined.Line 15
- Step 12:
JSON.stringify turns the object into text and JSON.parse builds a new object from it. The Date comes back as a plain string, and
noteis gone.Line 16
- Step 13:
It prints string false. JSON keeps only what JSON can express: no Dates, no undefined, no functions.
Line 17
- Step 14:
Output: Ali Giza, Giza Alex, string false. Spread copies one level, structuredClone copies every level, and JSON loses anything that is not plain data.