Deep and Shallow Copy in JavaScript: An In-Depth Guide
Chanchal Verma
Posted on May 2, 2023
When working with JavaScript, you may come across situations where you need to make copies of objects or arrays. This is where the concepts of deep and shallow copying come into play. In this guide, we will explore what deep and shallow copying are, why they are important, and how to implement them in your code.
What is Copying?
Before we dive into the specifics of deep and shallow copying, it's important to understand what copying means in the context of JavaScript. In simple terms, copying refers to the process of creating a new object or array with the same values as an existing one.
There are two main types of copying in JavaScript: shallow copying and deep copying.
Shallow Copying:
Shallow copying refers to creating a new object or array with the same values as the original, but the new object or array only holds references to the original object's or array's values. This means that any changes made to the original object or array will also be reflected in the copied object or array.
For example, consider the following code:
const originalArray = [1, 2, 3, 4];
const newArray = originalArray;
In this case, newArray is a shallow copy of originalArray. Any changes made to the originalArray will also be reflected in newArray.
Why use Shallow Copying?
Shallow copying can be useful in situations where you need to create a new object or array quickly and efficiently. Since a shallow copy only holds references to the original object's or array's values, it requires less memory than a deep copy.
Shallow copying is also useful when working with large objects or arrays, as creating a deep copy of a large object or array can be a time-consuming process.
How to Implement Shallow Copying in JavaScript?
There are several ways to implement shallow copying in JavaScript. Here are some examples:
Spread operator:
The spread operator is a popular method for creating shallow copies of objects and arrays. Here is an example:
const originalObject = {a: 1, b: 2, c: 3};
const newObject = {...originalObject};
const originalArray = [1, 2, 3, 4];
const newArray = [...originalArray];
In both cases, the spread operator is used to create a shallow copy of the original object or array.
Object.assign():
Another method for creating shallow copies of objects is to use the Object.assign() method. Here is an example:
const originalObject = {a: 1, b: 2, c: 3};
const newObject = Object.assign({}, originalObject);
In this case, Object.assign() is used to create a new object that is a shallow copy of the original object.
Deep Copying:
Deep copying, on the other hand, refers to creating a new object or array with the same values as the original, but the new object or array holds new copies of the original object's or array's values. This means that any changes made to the original object or array will not be reflected in the copied object or array.
For example, consider the following code:
const originalArray = [1, 2, 3, 4];
const newArray = JSON.parse(JSON.stringify(originalArray));
In this case, newArray is a deep copy of originalArray. Any changes made to originalArray will not be reflected in newArray.
Why use Deep Copying?
Deep copying is useful in situations where you need to create a new object or array with independent values from the original object.
For suh more Coding content you can follow me on
Github
LinkedIn
DEV
Posted on May 2, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.