structuredClone for Deep Copies
Clone complex objects safely.
The Deep Copy Problem
Spread and Object.assign make shallow copies: nested objects are shared by reference. To fully duplicate a nested structure you need a deep copy.
structuredClone is the built-in answer.
Why Spread Is Not Enough
A spread copy shares nested objects. Mutating the nested part of the copy also affects the original.
const original = { user: { name: 'Ada' } };
const shallow = { ...original };
shallow.user.name = 'Grace';
console.log(original.user.name);All lessons in this course
- Object.freeze and seal
- Shallow vs Deep Freezing
- Immutable Update Patterns
- structuredClone for Deep Copies