structuredClone()

raghavtandulkar's avatar
Published Feb 24, 2025
Contribute to Docs

In JavaScript, the structuredClone() method under the Window interface creates a deep copy of a JavaScript value, supporting circular references and built-in types that cannot be handled by the stringify() and parse() methods in JSON.

Syntax

structuredClone(value)

This method returns a new deep copy of the provided value, with all nested objects and special types properly cloned.

Example

The following example shows how structuredClone() deep copies a nested object, preserving the original structure while allowing independent modifications:

// Create an object with nested properties
const original = {
numbers: [1, 2, 3],
nested: {
name: 'Example',
},
};
// Clone the object
const clone = structuredClone(original);
// Modify the clone
clone.numbers.push(4);
clone.nested.name = 'Modified';
console.log(original.numbers);
console.log(clone.numbers);
console.log(original.nested.name);
console.log(clone.nested.name);

The output of the above code will be:

[1, 2, 3]
[1, 2, 3, 4]
Example
Modified

All contributors

Contribute to Docs

Learn JavaScript on Codecademy