Use structuredClone for Safe Deep Copying in JavaScript
Copying JavaScript values is easy until nested objects, dates, maps, sets, binary data, or circular references appear. The common JSON round trip works only for a limited subset of values and silently changes some data.
The built-in structuredClone() API provides a defined deep-cloning algorithm for many JavaScript data types.
Spread syntax is only a shallow copy Object spread copies the first level:
Copy const original = { profile: { name: "Ada" } }; const copy = { ...original }; copy.profile.name = "Grace"; console.log(original.profile.name); // "Grace" Both objects still reference the same nested profile. Spread syntax is useful when shallow copying is intentional, but it is not a general deep-copy mechanism.