What is the difference between a shallow copy and a deep copy with JavaScript arrays? [duplicate]

To see the difference, try:

shallow_copy[0][2] = 4;
console.dir(test);

You’ll see that test has been modified! This is because while you may have copied the values to the new array, the nested array is still the same one.

A deep copy would recursively perform shallow copies until everything is a new copy of the original.

Leave a Comment