0Pricing
JavaScript Academy · Lesson

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

  1. Object.freeze and seal
  2. Shallow vs Deep Freezing
  3. Immutable Update Patterns
  4. structuredClone for Deep Copies
← Back to JavaScript Academy