structuredClone - Web documentation
function structuredClone
structuredClone<T = any>(
value: T,
): T

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

Unlike a shallow copy, a deep copy does not hold the same references as the source object, meaning its properties can be changed without affecting the source. For more details, see MDN.

Throws a DataCloneError if any part of the input value is not serializable.

Examples

Example 1

const object = { x: 0, y: 1 };

const deepCopy = structuredClone(object);
deepCopy.x = 1;
console.log(deepCopy.x, object.x); // 1 0

const shallowCopy = object;
shallowCopy.x = 1;
// shallowCopy.x is pointing to the same location in memory as object.x
console.log(shallowCopy.x, object.x); // 1 1

Type Parameters

T = any

Parameters

value: T
optional
options: StructuredSerializeOptions

Return Type

T