JavaScript Beginner

Deep Clone an Object

Modern browsers and Node provide structuredClone, which deep-copies most values without the pitfalls of the old JSON.parse(JSON.stringify(...)) trick.

JavaScript
const original = { user: { name: 'Ada' }, tags: ['a', 'b'] };
const copy = structuredClone(original);

copy.user.name = 'Grace';
console.log(original.user.name); // 'Ada' — unchanged

Usage notes

structuredClone handles dates, maps, sets and nested arrays. It cannot clone functions or DOM nodes.