Priyanka Negi
2 min readOct 7, 2018

Deep Copy Vs Shallow Copy

Have you heard about Deep copy and Shallow copy ???
Obviously, yes many times you must have heard about deep copy and shallow copy in your tech career.

This blog is about Deep and Shallow copy, I am going to discuss about Deep and Shallow copy in brief so that things make sense to you easily.

What is Shallow Copy?
Shallow Copy is like, duplicate data from one place to another place, in programming terms you are just copying the address of that object means you haven’t copied the data you are just pointing to that address.
let’s take an example:

let originalObject = {name: ‘thejsgirl’};let shallowObject = originalObject; //shalow copynow, shallowObject.name=’devgirl’;
console.log(shallowObject.name);
//output: devgirl
console.log(originalObject.name);
//output: devgirl

So, in this example, we are just referencing to originalObject in this way both object values are affecting. In simple words both the objects referencing to the same copy of an object that’s why any object will change the object’s property value that will affect the other object’s properties.

here is one more example:

Here you can see both the object referencing to the same object if object2 changes the value of its property it automatically changes the value of the object1 property.

What is Deep Copy??
Deep Copy is like to copy the whole object’s properties from one object to another object in this way changing property of one object won’t affect the property of other objects.
Here is one example:

let originalObject = {name: 'thejsgirl'};
let shallowObject = JSON.parse(JSON.stringify(originalObject)); //deep copy
shallowObject.name = "devgirl"
console.log(shallowObject.name);
//output: devgirl
console.log(originalObject.name);
//output: thejsgirl
This is very efficient way to copy one object property to another.

let’s take one more example:

here you can see both the objects has their own refObj so they won’t effect the each other property’s value.

I hope this article is useful for you. I will try to improve it. Thanks for reading keep coding $ :-) $

Priyanka Negi

Diving into the ocean of JavaScript, TheJSGirl on Github