Deep Clone Object
Create a deep copy using structuredClone or JSON.
Code
JavaScript
// Modern: structuredClone (Node 17+, browsers)
const cloned = structuredClone(obj);
// Fallback: JSON (loses functions, undefined, Date)
const cloned2 = JSON.parse(JSON.stringify(obj));
// Shallow clone
const shallow = { ...obj };Line-by-line explanation
- 1.structuredClone handles most types including Date, Map, Set.
- 2.JSON method fails on functions, undefined, circular refs.
- 3.Spread {...obj} only copies top-level properties.
Expected output
Deep copy of obj