Skip to main content
Web Development

JavaScript Shallow Copy and Deep Copy

Ravi Kant Ranjan Ravi Kant Ranjan, Principal Developer - SAP Fiori, BTP 4 min read

In this blog, I will discuss the Shallow and Deep Copy of JavaScript arrays and objects.

Shallow Copy:

A shallow copy refers to specific values that are still connected to the original Variable (Array or Object). Every change to a new variable will affect the original variable.

Deep Copy:

A deep copy means that all the values of the new variable are copied to a new variable and disconnected from the original variable. All changes to a new variable will not affect the original variable.

Let us understand each with examples.

Code: marks array of five numbers assigned to marks2, then both logged to the console

Browser console showing both arrays as [10, 20, 30, 40, 50]

Note: The variables ‘marks’ and ‘marks2’ will have the same output.

Code: marks2 = marks, then marks.push(60) and marks2[0] = 5, both logged

Browser console showing both arrays as [5, 20, 30, 40, 50, 60]

The variables ‘marks’ and ‘marks2’ will have the same output because both variables point to the same reference/memory. Therefore, the example above, example array elements 60 and 5, is reflected in both variables.

So far, I have shown how copying arrays and modifying them reflects changes in both variables. A shallow copy can be done in the following ways:

Using the .concat Method

Code: marks2 = [].concat(marks), then marks.push(60) and marks2[0] = 5

Browser console: marks is [10, 20, 30, 40, 50, 60] and marks2 is [5, 20, 30, 40, 50]

Here the output is different in each variable. We took an empty array and then concatenated it with the original array. So modifying either of them is not affecting the other.

Using Array.from Method

Code: marks2 = Array.from(marks), then marks.push(60) and marks2[0] = 5

Browser console: marks is [10, 20, 30, 40, 50, 60] and marks2 is [5, 20, 30, 40, 50]

The Array.from() static method creates a new, shallow-copied Array instance from an iterable or array-like object.

Using the Slice Method

Code: marks2 = marks.slice(0), then marks.push(60) and marks2[3] = 15

Browser console: marks is [10, 20, 30, 40, 50, 70, 60] and marks2 is [10, 20, 30, 15, 50, 70]

The Slice method gives a new array, so the output for each array is different and not affected by each other.

Using Spread operator (…)

Code: marks2 = [...marks], then marks.push(60) and marks2[0] = 5

Browser console: marks is [10, 20, 30, 40, 50, 60] and marks2 is [5, 20, 30, 40, 50]

The spread operator method also creates a new array, so the output differs in each array, and any modification can affect either.

Up until now, we have seen how shallow copy works in the case of arrays. Now we will see the same in the case of objects.

Code: a Student object with id, name and age assigned to Student2, then both logged

Browser console showing both objects as {id: 100, name: 'Ajeet', age: 28}

Copying one object into another gives the same output.

Code: Student2 = Student, then Student.age set to 30 and Student2.name set to Jane

Browser console showing both objects with name 'Jane' and age 30

Copying one object into another and modifying either will affect each of them; hence, the output will be the same.

Now we will see how to do Shallow Copy in case of an object.

Using the Assign Method

Code: Student2 = Object.assign({}, Student), then Student.age set to 30 and Student2.name set to Doe

Browser console: Student has name 'John' and age 30, Student2 has name 'Doe' and age 28

The assign method creates a new object. It takes two arguments. One is an empty object {}, and the second is some object. Hence the output is different in both variables, which does not affect one or the other on modifying either.

Using Spread operator

Code: Student2 = { ...Student }, then Student.age set to 30 and Student2.name set to Jackey

Browser console: Student has name 'Mickey' and age 30, Student2 has name 'Jackey' and age 28

The spread operator(…) creates a new object. Hence the output is different in both variables, which does not affect each other by modifying either.

Let’s look at how Deep Copy works in Arrays and Objects.

Code: Student with a marks array copied with Object.assign, then the marks array changed through both objects

Browser console: both objects show marks [0, 2, 3, 4, 5, 100] though name and age differ

Here, you can see that changing the object’s property in each object does not affect the other, but changing the array’s value inside the object affects the array in both objects.

We added 100 in the last position of the 1st object’s array, which also added 100 in the last position of the 2nd object’s array. Similarly, We added 0 in the first position of the 2nd object’s array, which also added 0 in the first position of the 1st object’s array. 

Code: Student with a marks array copied with the spread operator, then the marks array changed through both

Browser console: the Jack and Naive objects both show marks [0, 2, 3, 4, 5, 100]

With the spread operator, it behaves in the same way as shown above.

So, here, the shallow copy doesn’t seem to work. This is where we bring in the concept of a deep copy.

Code: Student copied with JSON.stringify and JSON.parse, then properties and marks changed on both

Browser console: Mack has marks [1, 2, 3, 4, 5, 100] and Harry has marks [0, 2, 3, 4, 5]

Converting the array into a string, then converting it back to an object and storing it into another variable disconnects both objects. Hence, modifying the object’s property and array’s element inside each object will give a different output for each object.

Code: an array holding a nested array copied with the spread operator, then the nested array changed

Browser console: both arrays show the nested array as [100, 2, 3, 4, 5, 1000]

Similarly, in the case of arrays, the shallow copy fails if there is a nested array.

Code: an array holding a nested array copied with JSON.stringify and JSON.parse, then both changed

Browser console: nested array is [100, 2, 3, 4, 5] in marks and [1, 2, 3, 4, 5, 1000] in marks2

Converting the array to a string and then back to the object also creates a Deep copy in the array.

So, we learned what are Shallow and Deep copies of arrays and objects are. How to create Shallow create the Shallow and Deep copy of arrays and objects. Different ways of creating Shallow copies of objects and arrays and flaws with Shallow copy.

These are the topics in JavaScript that many developers overlook as a result of code breaks in the future, leading to the app crash. I hope these concepts help you in your development journey in JavaScript.

Happy Coding!!

If you’re interested in viewing similar content, you can visit our blog library here. 

View our LinkedIn here. 

Share