Deep vs Shallow Copy
A shallow copy of an object or array is a new container whose top-level values are copied from the original. Any nested objects or arrays inside it are not copied, so the copy and the original share them. A deep copy copies every level, so nothing is shared and a change to the copy cannot reach the original.
Copying is needed at all because assigning an object to another variable copies only a reference to it, as Pass By Reference explains.
Shallow copies
Section titled “Shallow copies”The spread syntax is the usual way to make a shallow copy. Changing a top-level property of the copy leaves the original alone, but changing a nested array changes the one array that both objects hold.
const recipe = { name: 'Pancakes', serves: 4, ingredients: ['flour', 'eggs', 'milk'],};
const copy = { ...recipe };copy.serves = 2;copy.ingredients.push('sugar');
console.log(recipe);console.log(copy.ingredients === recipe.ingredients);This prints:
{ name: 'Pancakes', serves: 4, ingredients: [ 'flour', 'eggs', 'milk', 'sugar' ]}trueThe original still serves 4, but it has gained sugar. Assigning a new array to copy.ingredients would have been safe, because that replaces the copy’s reference rather than changing the shared array.
Arrays have several ways to make a shallow copy, and they all give the same result:
const scores = [10, 20, 30];
const a = [...scores];const b = scores.slice();const c = Array.from(scores);const d = Object.assign([], scores);
a.push(40);console.log(scores, a, b, c, d);This prints:
[ 10, 20, 30 ] [ 10, 20, 30, 40 ] [ 10, 20, 30 ] [ 10, 20, 30 ] [ 10, 20, 30 ]For objects, Object.assign({}, source) does the same as { ...source }. A shallow copy is enough whenever every value at the top level is a primitive.
Deep copies with structuredClone
Section titled “Deep copies with structuredClone”structuredClone() makes a deep copy. It is built into every current browser and into Node 17 and later, and it handles nested objects and arrays as well as Date, Map, Set, typed arrays and objects that refer to themselves.
const recipe = { name: 'Pancakes', ingredients: ['flour', 'eggs', 'milk'], added: new Date('2025-03-14T09:30:00Z'), tags: new Set(['breakfast']),};
const clone = structuredClone(recipe);clone.ingredients.push('sugar');
console.log(recipe.ingredients);console.log(clone.ingredients);console.log(clone.added instanceof Date, clone.tags.has('breakfast'));This prints:
[ 'flour', 'eggs', 'milk' ][ 'flour', 'eggs', 'milk', 'sugar' ]true trueIt cannot copy functions or DOM nodes, and throws a DataCloneError when it meets one. It also does not keep an object’s prototype, so a class instance comes back as a plain object.
try { structuredClone({ scale: (n) => n * 2 });} catch (err) { console.log(err.name);}This prints:
DataCloneErrorThe JSON round trip
Section titled “The JSON round trip”Before structuredClone existed, the common way to deep copy was JSON.parse(JSON.stringify(value)). It still works for plain data made of strings, numbers, booleans, null, arrays and plain objects, but it quietly loses anything else.
const recipe = { name: 'Pancakes', added: new Date('2025-03-14T09:30:00Z'), tags: new Set(['breakfast']), rating: undefined, scale(n) { return n * 2; },};
const viaJson = JSON.parse(JSON.stringify(recipe));console.log(viaJson);This prints:
{ name: 'Pancakes', added: '2025-03-14T09:30:00.000Z', tags: {} }The date has become a string, the set has become an empty object, and the undefined property and the method have gone. It also throws on an object that refers to itself. Prefer structuredClone for new code, and write a custom copy function only when you need to copy something it refuses, such as a class instance with methods.
Steve Griffith covers this topic in his video Deep Copying vs Shallow Copying.