structuredClone()

Creates a deep copy of a value using the structured clone algorithm.

Since Node 17 Spec ↗

Syntax

structuredClone(value[, options])

Parameters

NameTypeRequiredDescription
value any Yes The value to deep-clone.
options object No `{ transfer: [...] }` to transfer ownership of transferable objects like ArrayBuffers.

Returns

any — A deep, independent copy of the input.

Examples

const original = { a: 1, nested: { b: [1, 2] }, when: new Date() };
const copy = structuredClone(original);
copy.nested.b.push(3);
console.log(original.nested.b, copy.nested.b);
Output
[ 1, 2 ] [ 1, 2, 3 ]
const m = new Map([['k', { v: 1 }]]);
const c = structuredClone(m);
console.log(c.get('k') === m.get('k'));
Output
false

Notes

Handles cyclic references, Map, Set, Date, and typed arrays - things `JSON.parse(JSON.stringify())` cannot. It CANNOT clone functions, DOM nodes, or class prototypes (throws `DataCloneError` for functions).

See also