structuredClone
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
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
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
DateMap,SetBlob,File,ArrayBuffer,TypedArrayRegExp(preserves the pattern and flags)- even circular references (unlike
JSON.stringify())
Example with Map and Date
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')); // 1Example with a circular reference (it works!)
const obj = {};
obj.self = obj;
const clone = structuredClone(obj);
console.log(clone.self === clone); // truestructuredClone() 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
const obj = {
fn: () => {},
el: document.body
};
structuredClone(obj);
// Error: function or DOM node cannot be clonedComparison 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:
npm install core-jsSUMMARY
| 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 |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.