DEV Community

Cover image for Javascript Shallow Copy - what is a Shallow Copy?

Javascript Shallow Copy - what is a Shallow Copy?

Johnny Simpson on October 22, 2022

Shallow copy and deep copy are terms thrown around in Javascript that can be confusing if you have never heard them before. It is quite common to h...
Collapse
 
caioragazzi profile image
Caio Ragazzi

Thanks for sharing! I didn't know the structuredClone() method... never used it before!

Thanks

Collapse
 
3ankur profile image
Ankur V

Thank you for sharing :)

Collapse
 
llorx profile image
Jorge Fuentes • Edited

"This can give the illusion that these two arrays are copies and act independently of each other - but that's not the case either."

This is a false claim. They are copies. When you slice, the array is copied to another section (the values and the pointer references). Each array has its own section of memory that do not overlap with the other section.

What you are talking about are pointer references to objects, which are also stored in another section of the memory. When you slice, those pointer references are copied, so the element 0 in both arrays point to the same object, but that pointer in the element 0 is stored in different parts of the memory, hence copied. When you do arr[0].X, the .X means to follow the pointer to the object and access the property X, hence both arrays will access the same object as each array has the same pointer reference in the position 0, but you can modify that pointer reference in one array with arr[0] = obj2 and it will not affect the other array because the position 0 is stored in different parts of the memory. Now each array will point to a different object in the position 0 because you modified the memory that stores the pointer in the position 0 only in one of the arrays.

Collapse
 
smpnjn profile image
Johnny Simpson

correct yes, i am referring to them being both copies and acting independently as being false. being copies alone is true.

Collapse
 
methe1 profile image
mohammed ezzitouni

Right on! slice's doing a deep copy, cuz a shallow copy would be pointers pointing to the start, and the end indices are given to the slice function, that way you have direct access to the array where the beginning and the end of it are different than the original start (0) & end (length -1).

Collapse
 
hudson3384 profile image
Hudson Arruda

The ES6 version add the spread function :
Let arr = [1,2,3]
Let arr2 = [...arr]
Arr.push(4)
Console.log(arr) // 1,2,3,4
Console.log(arr2) // 1,2,3

Collapse
 
smpnjn profile image
Johnny Simpson

the spread syntax still only makes a shallow copy.

Collapse
 
chadgauth profile image
Chad Gauthier • Edited

It's a deep copy if the data is not nested, if it is nested those are shallow:

const arr = [1,2,[1,2]]
const arr2 = [...arr]
arr[2].push(3)
console.log(arr2) // 1,2,[1,2,3]

Thread Thread
 
smpnjn profile image
Johnny Simpson

no spread syntax creates a shallow copy of data with the same ref in memory. That's the main differentiator between a deep and shallow copy - their ref in memory. In fact pretty much all array methods create shallow copies.

Collapse
 
fyodorio profile image
Fyodor

FWIW the way of using JSON methods is not the best one, you might loose some data in cloned objects

Collapse
 
smpnjn profile image
Johnny Simpson

Correct, I've added that note.

Collapse
 
davidyaonz profile image
David Yao

Sometimes bugs were found when implementing spread operator, it happens to me as storms all the time.

Collapse
 
azzarox profile image
Azzarox

Basically, if you create shallow copy and use mutating function or smth on it, it should change the original as well?

Collapse
 
smpnjn profile image
Johnny Simpson

In ideal circumstances, yes - unless you do something like the example above where you slice the array and then update using the square bracket notation.