What are the different ways to copy an object or an array?
TL;DR
Use spread syntax or Object.assign() for a shallow copy. Use structuredClone() for values supported by the structured clone algorithm, including nested arrays and objects, cycles, Map, Set, and many built-in types. A JSON stringify/parse round trip is only a lossy conversion for JSON-shaped data, not a general deep-clone algorithm. A library or domain-specific copy routine may be needed for custom classes and semantics.
Different ways to copy an object or an array
Shallow copy
Using the spread operator
The spread operator (...) is a concise way to create a shallow copy of an array or an object.
Using Object.assign()
Object.assign() can also be used to create a shallow copy of an object.
Deep copy
Using structuredClone()
structuredClone() is a built-in function (Baseline across modern browsers and available in Node.js 17+) that creates a deep copy. It handles nested objects, arrays, Date, Map, Set, and circular references, but it cannot clone functions or DOM nodes (those throw a DataCloneError).
Using JSON.parse(JSON.stringify())
This can copy JSON-shaped data, but it is lossy: it drops unsupported object properties such as functions, undefined, and symbols; converts dates through toJSON(); does not preserve Map, Set, prototypes, or special numeric values faithfully; throws for BigInt and cycles; and can invoke custom toJSON() behavior.
Using Lodash's _.cloneDeep()
Lodash is a popular utility library that provides a _.cloneDeep() method for deep copying objects and arrays.
Other methods
Using recursion
For custom deep copy logic, you can implement a recursive function.
Further reading
- MDN Web Docs: Spread syntax
- MDN Web Docs: Object.assign()
- MDN Web Docs: structuredClone()
- Lodash documentation: _.cloneDeep
- MDN Web Docs: JSON.parse()
- MDN Web Docs: JSON.stringify()