Shallow copy vs Deep copy
Shallow copy vs Deep copy in JavaScript

Shallow copy vs Deep copy

Copy to an object can be created in many ways in JavaScript. Let us first understand difference between primitives and non-primitives.

When we create a variable to hold primitive data, then that variables hold that data.

let data=200

When a variable is assigned with non-primitives like array or object, that variable holds reference to that non-primitive ,instead of that as value. This is very important point to notice when creating copy of an object.

Let us create a simple "student" object and its copy. To create copy we can use either Object.assign() or spread operator(...).

No alt text provided for this image



The above two objects will occupy different memory locations like below.

No alt text provided for this image

As of now, we are very clear with copying of objects.

Now ,let me add some non-primitives to student object like marks property with an array and address property with an object.

No alt text provided for this image

Here, the student object has two properties as references to other objects.(Array is also an object). The copy of student object(copyOfStudent) will get all properties , but , the properties marks and address will refer to same objects which are referred by student object ,instead of creating new copies of those. The following diagram illustrates that fact.


No alt text provided for this image

when we change the values of properties rollno,name and age, those changes will be reflected only in that object.But when change values of either marks or address, those changes will be reflected for both student and copyOfStudent objects. This is beacause, these two objects do not have separate copies of those two(marks array and address object).

This is called Shallow copy. It means ,copy of an object copies the ‘main’ object, but doesn’t copy the inner objects. The ‘inner objects’ are shared between the original object and its copy.

Deep copy

Unlike the shallow copy, a deep copy is a fully independent copy of an object. If we copied our student object, we would copy the entire object structure.

We can use a simple technique to achieve this.

Convert JavaScript object (student) to string.

For that, we can use JSON.stringify() method which takes JavaScript object as argument and returns its string representation.

Then, convert this string back to object using JSON.parse() method. Now, it takes complete string representation of object and produce a new object as a complete copy of all primitive and non-primitive values.

const copyOfStudent=JSON.parse(JSON.stringify(student))

No alt text provided for this image

You can see that, the copy object also has its own copy of all properties including primitives and non-primitives. This is called Deep copy.

Change the values in copy object(copyOfStudent) and compare main object(student) for better understanding.


Thanks for reading this article and hope you clear with the concept of shallow copy and deep copy

要查看或添加评论,请登录

Rajesh T的更多文章

  • What is an API ?

    What is an API ?

    An API is a set of rules and protocols that allow different software applications to communicate with each other. Types…

    1 条评论
  • Redux vs Zustand for state management in React app

    Redux vs Zustand for state management in React app

    "A JS library for predictable and maintainable global state management" - Redux "A small, fast, and scalable bearbones…

    1 条评论
  • Simple state management in MEAN Application

    Simple state management in MEAN Application

    State management in Web apps is always challenging. Here, I have taken a simple e-learning app to demonstrate, state…

    1 条评论
  • Units in CSS

    Units in CSS

    Absolute length units The absolute length units are fixed and a length expressed in any of these will appear as exactly…

  • Promises in JavaScript

    Promises in JavaScript

    The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting…

  • Callbacks in JavaScript

    Callbacks in JavaScript

    Most of us feel confusion while learning about asynchronous behavior of JavaScript. In this article i tried to explain…

  • Error handling in Express Application

    Error handling in Express Application

    Error Handling refers to how Express catches and processes errors that occur both synchronously and asynchronously…

    1 条评论
  • Storing images in MEAN stack application using "Cloudinary"

    Storing images in MEAN stack application using "Cloudinary"

    Saving images and videos of a web application is always challenging, because, performance of application matters.We…

    2 条评论
  • Connecting Angular application with Nodejs backend

    Connecting Angular application with Nodejs backend

    In this article, i will explain ,how to connect Angular Application with Nodejs backend. When we create a new angular…

  • Token based Authentication in ExpressJS Application

    Token based Authentication in ExpressJS Application

    HTTP is a stateless protocol. That means ,it cannot maintain state of previous requests.

社区洞察

其他会员也浏览了