Deep Clone an Object
Deep Clone an Object is a reusable JavaScript snippet. Creates a deep copy of a JavaScript object, handling nested objects and arrays. Note: does not handle functions, symbols, or circular references. The full implementation: // Method 1: JSON (simple, no functions/symbols) const deepClone = (obj) => JSON.parse(JSON.stringify(obj)) // Method 2: Recursive (handles more cases) function deepClone(obj) { if (obj === null || typeof obj !== 'object') return obj if (obj instanceof Date) return new Date(obj) if (obj instanceof Array) return obj.map(item => deepClone(item)) const cloned = {} for (const key in obj) { if (obj.hasOwnProperty(key)) { cloned[key] = deepClone(obj[key]) } } return cloned } // Usage const original = { a: 1, b: {… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.