Skip to content
YouTurkey Tech
All JavaScript topics

Shallow vs Deep Copy

Beginner

Why changing a copy made with spread can still change the original, and what structuredClone and JSON copy instead.Builds on Primitive vs Reference Types

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/14Press Play, or step through with the arrows, to begin.
Step 0 of 14

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() and Array.from(arr). They are fast and fine for flat data.
  • Deep: structuredClone(obj). It copies nested objects, arrays, Date, Map, Set and even circular references. It throws a DataCloneError on 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 Date becomes a string;
    • undefined, functions and symbols are dropped (inside arrays they become null);
    • NaN and Infinity become null;
    • Map and Set become {};
    • a circular reference or a BigInt throws a TypeError.

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); // true

Quick 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
  1. Step 1:

    The global context starts in its creation phase.

  2. Step 2:

    user, shallow, deep, event and copy are reserved but uninitialized, as every const starts.

    Lines 1–16

  3. Step 3:

    A nested object is two objects in the heap. user points to the outer one, and its address holds a reference to the inner one.

    Lines 1–4

  4. Step 4:

    Spread creates a new object but copies only the top level. address is copied as a reference, so both objects share the same address.

    Line 6

  5. Step 5:

    shallow.name = 'Sara' changes the copy's own property. user.name is still 'Ali'.

    Line 7

  6. Step 6:

    shallow.address leads to the shared inner object, so changing its city changes it for user too.

    Line 8

  7. Step 7:

    It prints Ali Giza. The top level was copied, the nested level was shared: that is a shallow copy.

    Line 9

  8. Step 8:

    structuredClone copies every level. deep gets a new outer object and its own address object.

    Line 11

  9. Step 9:

    deep.address.city = 'Alex' changes only the clone's address.

    Line 12

  10. Step 10:

    It prints Giza Alex: the original is untouched.

    Line 13

  11. Step 11:

    event holds a Date object and a property set to undefined.

    Line 15

  12. 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 note is gone.

    Line 16

  13. Step 13:

    It prints string false. JSON keeps only what JSON can express: no Dates, no undefined, no functions.

    Line 17

  14. 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.