Deep and Shallow Copy with Object.assign JavaScript

Mayank Gupta
TechnoFunnel
Published in
3 min readSep 13, 2020

--

Working with JavaScript Object.assign Function

Deep and Shallow Copy in JavaScript with Object.assign, Cloning Data in JavaScript, Copy Data in JavaScript
Deep and Shallow Copy in JavaScript with Object.assign

TechnoFunnel presents another article focussed on Cloning an Object using Object.assign Function in JavaScript. Object.assign is another great way to copy the object into some other Object. We will look whether we can perform Deep or Shallow Copy using this method.

For other Methods of copying Object, you can refer to the following:

What is Object.assign in JavaScript

Object.assign is used to copy the key value pair of one object to another. Lets look for the below code to understand the working of this keyword.

In the above code, we have created a new blank object using ”{}” and then we are asking JavaScript to copy the key value pairs of “userDetails” Object into newly created blank object. The newly created object creates a separate memory space to save the values.

What is Object Cloning, Why do we do Object Cloning, Object Copy with Object.assign. Deep and Shallow Copy in JavaScript
Working with Object.assign

However, this keyword can also be used to copy the key value pairs from multiple Objects. After creating an empty Object, we can pass as many number of object as we want to this function. The key value pair of all these objects will be copied to this newly created Object. Lets see with the help of an Example below.

--

--

Mayank Gupta
TechnoFunnel

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue https://medium.com/technofunnel