structuredClone()
Creates a deep copy of many built-in types using the structured clone algorithm.
const copy = structuredClone(value)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Creates a deep copy of many built-in types using the structured clone algorithm.
structuredClone() creates a deep copy of a given value using the HTML structured clone algorithm. Unlike shallow copies created with the spread operator or Object.assign(), this method recursively copies nested properties, ensuring no shared references exist between the source and the clone. It is significantly more robust than the JSON.stringify() hack because it natively handles circular references and preserves complex built-in types such as Map, Set, Date, RegExp, and ArrayBuffer. However, it cannot clone functions, DOM nodes, or property descriptors like getters and setters. In terms of performance, it is highly optimized as a native runtime feature, making it preferable for large data structures, though it is slower than a shallow copy. It is available in modern browsers and Node.js 17+, but IE is not supported.
Quick reference
Syntax
const copy = structuredClone(value)
Inputs
Parameters
See it in practice
Examples
Deep Cloning Nested Objects
const original = { user: { id: 1, settings: { theme: 'dark' } } };
const copy = structuredClone(original);
copy.user.settings.theme = 'light';
console.log(original.user.settings.theme);
console.log(copy.user.settings.theme);"dark" "light"
Modifying a deeply nested property in the clone does not affect the original object, proving a true deep copy was performed.
Cloning Maps and Sets
const originalMap = new Map([['key', { a: 1 }]]);
const copyMap = structuredClone(originalMap);
copyMap.get('key').a = 99;
console.log(originalMap.get('key').a);
console.log(copyMap.get('key').a);1 99
Unlike JSON methods, structuredClone correctly clones Map and Set objects along with their contents.
Handling Circular References
const node = { name: 'root' };
node.self = node;
const copy = structuredClone(node);
console.log(copy !== node);
console.log(copy.self === copy);true true
The algorithm tracks previously visited objects, allowing it to recreate circular references in the clone without entering an infinite loop.
Debug faster
Common Errors
DataCloneError
Cause: Attempting to clone a value that is not serializable, such as a function or a DOM element.
Fix: Ensure the object contains only data. If functions are needed, they must be assigned manually after cloning.
const obj = { method: function() { return 1; } };
const copy = structuredClone(obj); // Throws DataCloneErrorLogicError
Cause: Cloning a class instance results in a plain object, losing the prototype chain and methods.
Fix: Manually re-instantiate the class or use a library that supports prototype preservation.
class User { constructor(n) { this.n = n; } }
const user = new User('Dev');
const copy = structuredClone(user);
console.log(copy instanceof User); // falseRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Creates a deep copy of many built-in types using the structured clone algorithm.
value: Value to deep-clone (must be clonable).
DataCloneError: Ensure the object contains only data. If functions are needed, they must be assigned manually after cloning. LogicError: Manually re-instantiate the class or use a library that supports prototype preservation.