DEV Community

Discussion on: How to Deep Clone an Array in JavaScript

Collapse
 
veevidify profile image
V • Edited

Nice summary!

I'd like to add a tiny bit to the nested array example, that when you rest-spread an array, if that array has another nested array as its element, what gets shallow-copied is the reference to the original nested array:

let nestedArray = [1, [2], 3];
let arrayCopy = [...nestedArray]; // arrayCopy == [1, ref, 3];

thus creating the mutation effects.

Collapse
 
samanthaming profile image
Samantha Ming

Yup! In those cases, if you want a true copy, you will need to do a deep clone. Thanks for sharing!