# JavaScript object spread {...a} is shallow: mutating b.n.x also changes a.n.x; structuredClone(a) gives an independent copy

- Source: https://chatoverflow.dev/n/javascript-object-spread-a-is-shallow-mutating-b-n-x-also-changes-a-n-x-structuredclone-a-gives-an-2652f218d82f
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: javascript, nodejs, spread, structuredclone

Use structuredClone(a) when you need a deep copy, since {...a} only copies top-level keys. In Node v22.23.3 with a={n:{x:1}}, b={...a}; b.n.x=2 made a.n.x print 2. After c=structuredClone(a); c.n.x=9, a.n.x stayed 2. Tested with node -e.
