Trivia Questions2 min readPremium

Shallow Copy vs Deep Copy in JavaScript

A shallow copy creates a new object but copies only the top level: nested objects are shared with the original. A deep copy duplicates every level, so the copy and the original share nothing.

const original = { name: 'Sam', address: { city: 'Auckland' } };

const shallow = { ...original };
shallow.name = 'Alex';              // original.name is still 'Sam'
shallow.address.city = 'Wellington'; // original.address.city is now 'Wellington'

const deep = structuredClone(original);
deep.address.city = 'Dunedin';       // original is untouched

1. Ways to make a shallow copy

const objCopy = { ...obj };
const objCopy2 = Object.assign({}, obj);

const arrCopy = [...arr];
const arrCopy2 = arr.slice();
const arrCopy3 = Array.from(arr);

All of these copy references for anything nested.

2. Ways to make a deep copy

structuredClone (the default choice)

const copy = structuredClone(value);

It handles nested objects and arrays, Date, Map, Set, RegExp, typed arrays and circular references. It does not handle everything:

  • Functions and DOM nodes throw a DataCloneError.
  • Class instances come back as plain objects: the prototype and methods are lost.
  • Getters, setters and non-enumerable properties aren’t preserved.

The JSON round trip

const copy = JSON.parse(JSON.stringify(value));

Works for plain data only. It silently changes or drops things:

Value After JSON round trip
Date ISO string
undefined, functions Removed
Map, Set {}
NaN, Infinity null
Circular reference Throws TypeError

A hand-written clone

Only worth it when you need behaviour the built-ins lack, such as keeping class prototypes or cloning functions by reference. It’s a classic interview task: deep clone and, with cycles, clone an object graph.

3. You often don’t need a deep copy

React and Redux need a new reference for everything that changed, not a copy of everything. Copy along the path you’re updating and reuse the rest:

const next = {
  ...state,
  user: {
    ...state.user,
    address: { ...state.user.address, city: 'Wellington' },
  },
};

next.settings === state.settings; // true: untouched branches are shared

This is called structural sharing. It’s cheaper than a deep copy, and it lets React.memo skip components whose data didn’t change. A deep copy gives every object a new reference, so everything re-renders.

4. Copying arrays of objects

const todos = [{ id: 1, done: false }];

const copy = [...todos];
copy[0].done = true;       // also changes todos[0]

const safe = todos.map((t) => (t.id === 1 ? { ...t, done: true } : t));

Choosing a method

Situation Use
Flat object or array Spread ({ ...obj }, [...arr])
Updating nested state in React or Redux Spread along the changed path
Independent copy of nested data structuredClone
Plain JSON data in an old environment JSON.parse(JSON.stringify(x))
Class instances or functions inside A custom clone

Follow-up questions to expect

  • “Is Object.assign deep?” No. It copies own enumerable properties one level down.
  • “How does a deep clone handle cycles?” Keep a WeakMap from original to copy, and return the existing copy when you meet the same object again.
  • “Does Object.freeze make a deep-frozen object?” No, it’s shallow too. See immutable vs mutable objects.