JavaScript

Deep Clone Object

by @admin
12h ago
Apr 28, 2026
Public
Creates a true deep copy of any serialisable object or array — nested objects, arrays, dates (as ISO strings). Uses structuredClone when available (modern browsers/Node 17+) and falls back to JSON round-trip for older environments. Does not handle functions, undefined, or circular references.
JavaScript
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
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.