- Shallow copy: when copying an array with objects, the object reference is copied into a new reference variable, and both point to the same object in memory. In this case, the source or copy changes can also affect the second object.
- Deep copy: when copying an array with objects, the source and copy become completely independent. Any changes to the source or copy will not affect the other object.
Copying array using the slice() method
Cloning array using the concat() method
Copying array using the from() method
Cloning array using the spread operator
Copying array using the map() method
Creating a deep clone with the structuredClone() method
Creating a deep copy with JSON.stringify() and JSON.parse()
The most popular method for deep copying arrays before structuredClone() was converting the array to a JSON string and then converting the JSON string back to an array. The main disadvantage of this method is its low performance and the limitation of working only with JSON-serializable content.
You can also use the array.map() method instead of a for loop: