Skip to main content

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

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

MethodCopy typeCopies nested dataCopies functionsCopies Date / Map / Set
{ ...obj }shallowNoYesNo
Object.assign()shallowNoYesNo
JSON.parse(JSON.stringify())deepYesNoNo
structuredClone()deepYesNoYes
_.cloneDeep() (Lodash)deepYesNoYes

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

PropertystructuredClone()
Copy typeDeep
Changes the originalNo
Copies objects, arrays, Map, Set, DateYes
Works with circular referencesYes
Copies functions / DOM elementsNo
Safe and built-inYes

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.