function deepClone(value) {
if (typeof structuredClone === 'function') {
return structuredClone(value);
}
return JSON.parse(JSON.stringify(value));
}
// Usage
const original = { a: 1, b: { c: [2, 3] } };
const copy = deepClone(original);
copy.b.c.push(4);
console.log(original.b.c); // [2, 3] — untouched
Create a free account and build your private vault. Share publicly whenever you want.