structuredClone
- Version
- es2025
- Review
- Draft
Summary#
structuredClone makes a deep copy using the structured clone algorithm. It preserves supported types such as Date, Map and Set, including circular references.
Syntax#
const copy = structuredClone(value);
Example#
const original = {
created: new Date(0),
tags: new Set(["draft"]),
author: { name: "Ada" },
};
const copy = structuredClone(original);
copy.author.name = "Lin";
copy.tags.add("final");
console.log(original.author.name, original.tags.size);
console.log(copy.created instanceof Date, copy.tags.size);
try {
structuredClone({ run: () => 1 });
} catch (error) {
console.log(error instanceof DOMException && error.name);
}
Expected output#
Ada 1
true 2
DataCloneError
Common mistakes#
Functions and symbol values throw DataCloneError. A cloneable instance of an ordinary class comes back as a plain object. Its prototype methods and private fields are omitted. Copy such objects with their own method when behavior must survive.
Version applicability#
structuredClone is a Web API defined by the HTML Standard. The complete example runs in Node.js 17.0.0 and later, where structuredClone and DOMException are globals.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0