Object cloning
suggest changeCopying an object together with its nested values is commonly called deep cloning. For values supported by the structured clone algorithm, use structuredClone():
const existing = {
createdAt: new Date(),
metadata: new Map([['priority', 1]]),
nested: { value: 2 },
};
const copy = structuredClone(existing);
existing.nested.value = 3; // copy.nested.value remains 2
structuredClone() supports circular references and built-in types such as Date, Map, Set, ArrayBuffer, and typed arrays. It cannot clone functions, DOM nodes, symbols, or every host-defined object; unsupported values cause a DataCloneError.
The older JSON round-trip technique is appropriate only when the data is deliberately JSON-compatible:
const jsonCompatible = { name: 'Ada', scores: [10, 20] };
const copy = JSON.parse(JSON.stringify(jsonCompatible));
JSON serialization loses values such as undefined, does not support BigInt or cycles, and converts types such as Date to strings. Neither technique preserves custom property descriptors or a user-defined prototype; specialized domain objects may need an explicit copy operation.