Sub topic 11
Spread Copies Are Shallow
This chapter assumes you know array and object spread from earlier chapters. It also assumes you know the difference between primitive values and references. Spread gives you a new outer array or object. Any object stored inside it is not copied. The copy holds a reference to that same inner object.
What "shallow" means
A shallow copy creates one new container and copies each top-level value into it. A primitive such as a number or string is copied by value. An object, array, or function is copied as a reference, so both containers end up pointing at the same thing.
const defaults = { theme: 'light', layout: { cols: 2 } };
const settings = { ...defaults, theme: 'dark' };
console.log(settings === defaults); // false (new outer object)
console.log(settings.layout === defaults.layout); // true (same inner object)
settings.layout.cols = 3;
console.log(defaults.layout.cols); // 3 (the change shows through)
settings.theme = 'blue';
console.log(defaults.theme); // 'light' (top-level primitive was copied)Arrays behave the same way
Array spread copies the elements into a new array. If those elements are arrays or objects, both arrays share them.
const rows = [[1, 2], [3, 4]];
const copy = [...rows];
copy.push([5, 6]);
console.log(rows.length); // 2 (outer arrays are separate)
copy[0].push(99);
console.log(rows[0]); // [1, 2, 99] (inner arrays are shared)Rest patterns work the same way. With const { a, ...others } = obj, the others object is new, but any nested values in it are still references into obj.
Common mistakes
| Mistake | What actually happens |
|---|---|
| Treating `{ ...obj }` as a full clone | Nested objects stay shared, so changing one changes the other |
| Spreading only the outer level when updating state | Writing to a nested property mutates the original |
| Using `JSON.parse(JSON.stringify(x))` for a deep copy | Dates turn into strings, and `undefined` values and functions are dropped |
Getting a deep copy
If you need fully independent data, use structuredClone. It is available in modern browsers and Node 17 and later.
const original = { created: new Date(0), tags: ['a'], meta: { n: 1 } };
const clone = structuredClone(original);
clone.meta.n = 2;
clone.tags.push('b');
console.log(original.meta.n); // 1
console.log(original.tags); // ['a']
console.log(clone.created instanceof Date); // truestructuredClone throws a DataCloneError on functions. It also does not keep class prototypes, so class instances come back as plain objects.
Often you don't need a full deep copy. You can spread at each level you change and keep sharing the levels you leave alone:
const updated = { ...defaults, layout: { ...defaults.layout, cols: 4 } };
console.log(updated.layout === defaults.layout); // falseThe chapter on practical patterns builds on this idea.
Exercise
Given const a = { list: [1], count: 0 }; const b = { ...a }; b.list.push(2); b.count++;, what are a.list and a.count?
Show answer
a.list is [1, 2], because b.list points to the same array. a.count is 0, because the number was copied by value and b.count++ changed only b.
Exercise
Write one expression that copies const cfg = { db: { host: 'x', port: 1 }, debug: false } with db.port set to 2, without changing cfg.
Show answer
const next = { ...cfg, db: { ...cfg.db, port: 2 } }; This gives next and next.db new objects. cfg.db.port stays 1.