DEV Community

Cover image for Shallow Copy vs Deep Copy in Javascript
Ahmed Nagi
Ahmed Nagi

Posted on

Shallow Copy vs Deep Copy in Javascript

Copying objects in javasceript to manipulate is common best practice.

✅ When you copy javascript objects or arrays keep in mind that only the outer properties are actually copied while the nested properties inside them only get referenced.

javascript default copy mode is shallow copy, to save on memory. But in the same time introduce a weird behavior.

💥When you try to change a property of a shallow copied object the change will effect the original object, that causes nasty bugs that get you stuck on for hours.

Shallow Copy vs Deep Copy in Javascript

Top comments (0)