Suggest an editImprove this articleRefine the answer for “structuredClone”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`structuredClone()`** is a built-in JavaScript function that makes a deep copy of any value, including complex objects, arrays, maps, and even dates. **Key point:** it is the most modern and safe way to clone data in JS without third-party libraries.Shown above the full answer for quick recall.Answer (EN)Image`structuredClone()` is a **built-in JavaScript function** that makes a **deep copy** of any value, including complex objects, arrays, maps, and even dates. This is the **most modern and safest way** to clone data in JS without third-party libraries. --- ## In short ```javascript const clone = structuredClone(value); ``` It creates a **fully independent copy** of the object, including all nested structures, and returns a new object with no shared references to the original. --- ## Example ```javascript const user = { name: 'Tim', address: { city: 'Berlin' }, hobbies: ['sport', 'music'] }; const clone = structuredClone(user); clone.address.city = 'Lviv'; clone.hobbies.push('coding'); console.log(user.address.city); // "Berlin" console.log(user.hobbies); // ['sport', 'music'] ``` The copy is **deep** - all nested objects and arrays are recreated. The original stays unchanged. --- ## What `structuredClone` can copy Supports: - objects (`{}`), arrays (`[]`) - nested structures of any depth - `Date` - `Map`, `Set` - `Blob`, `File`, `ArrayBuffer`, `TypedArray` - `RegExp` (preserves the pattern and flags) - even **circular references** (unlike `JSON.stringify()`) --- ### Example with Map and Date ```javascript const data = { date: new Date(), map: new Map([['a', 1], ['b', 2]]) }; const copy = structuredClone(data); console.log(copy.date === data.date); // false (a new object) console.log(copy.map.get('a')); // 1 ``` --- ### Example with a circular reference (it works!) ```javascript const obj = {}; obj.self = obj; const clone = structuredClone(obj); console.log(clone.self === clone); // true ``` `structuredClone()` handles even self-references, where `JSON.parse(JSON.stringify())` would simply fail with an error. --- ## What is **not copied** Does not support: - **functions** - **DOM elements** - **classes / prototypes** - **Symbol** - **undefined as a key** ```javascript const obj = { fn: () => {}, el: document.body }; structuredClone(obj); // Error: function or DOM node cannot be cloned ``` --- ## Comparison with other methods | Method | Copy type | Copies nested data | Copies functions | Copies Date / Map / Set | |---|---|---|---|---| | `{ ...obj }` | shallow | No | Yes | No | | `Object.assign()` | shallow | No | Yes | No | | `JSON.parse(JSON.stringify())` | deep | Yes | No | No | | `structuredClone()` | deep | Yes | No | Yes | | `_.cloneDeep()` (Lodash) | deep | Yes | No | Yes | --- ## Support Works: - in all modern browsers (Chrome 98+, Firefox 94+, Edge 98+, Safari 15.4+) - in Node.js 17+ - in Deno For older environments you can add a **polyfill**, for example: ```javascript npm install core-js ``` --- ## SUMMARY | Property | `structuredClone()` | |---|---| | Copy type | Deep | | Changes the original | No | | Copies objects, arrays, Map, Set, Date | Yes | | Works with circular references | Yes | | Copies functions / DOM elements | No | | Safe and built-in | Yes |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.