# structuredClone()

> The `structuredClone()` global JS method creates a deep copy of a value.

## How do I deep copy an object in JavaScript?

The global method that creates a deep copy of a value using the structured clone algorithm. Supports nested objects, `Map`, `Set`, `Date` and circular references, but not functions or DOM nodes.

- Topic: Specials
- Type: method
- Browser support: Chrome 98+, Chrome Android 98+, Edge 98+, Firefox 94+, Firefox Android 94+, Safari 15.4+, Safari iOS 15.4+

## Example

Wrong:

```js
const copy = JSON.parse(JSON.stringify(state));
copy.createdAt.getTime();
```

Right:

```js
const copy = structuredClone(state);
copy.createdAt.getTime();
```

The JSON round trip turns `Date` into a string; `structuredClone()` keeps `Date`, `Map`, `Set` and circular references.

## Common mistakes

- Cloning a class instance and getting a plain object without its prototype.

## Key points

- Deep copies use `structuredClone()`, not `JSON.parse(JSON.stringify())`, which loses `Date`, `Map` and `Set`.
- The value has no functions or DOM nodes, which throw a 'DataCloneError'.
- Code does not expect class instances to keep their methods after cloning.

## Interview questions

### Does structuredClone() copy functions?

No, functions and DOM nodes throw a 'DataCloneError'. Remove them or copy those fields by hand.

### structuredClone vs spread syntax: what is the difference?

Spread makes a shallow copy, so nested objects are shared. `structuredClone()` copies every level.

### Does structuredClone() keep getters and class prototypes?

No, getters are read into plain values, and class instances become plain objects without their methods.

## Related cards

- [Deep Copy](https://wonderium.dev/js/deep-copy.md)
- [Shallow Copy](https://wonderium.dev/js/shallow-copy.md)
- [Spread Syntax](https://wonderium.dev/js/spread-syntax.md)
- [JSON](https://wonderium.dev/js/json.md)
- [BroadcastChannel](https://wonderium.dev/js/broadcast-channel.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/structured-clone
- Last updated: 2026-11-25
