DEV Community

Himanshupal0001
Himanshupal0001

Posted on

Shallow Copy vs Deep Copy JS & Reference Part-3πŸš€

his post is continuation of the last post.

In last post we were discussing What is Reference and done with assign by reference. Now we discuss further.

Table of content

1 Primitive Data type
2 Non-primitive data type
3 What is Reference
4 Shallow copy
5 Deep Copy

What is Shallow Copy?

Shallow copy is creating new copy of an array to the variable. The goal is to just copy the values of an array assign to the new variable which make it a new array rather pointing to the same memory. It is crucial in crud operations because they are immutable.

See the example below.

const student = {
  name: 'Sid',
  class: 'KG',
  subject: 'JS',
  marks: '100'
}
console.log(student);
/* output ->
name:"Sid"
class:"KG"
subject:"JS"
marks:"100"
*/

const studentCopy = {...student}
console.log(studentCopy);
/* output ->
name:"Sid"
class:"KG"
subject:"JS"
marks:"100"
*/

studentCopy.name = 'Alic'
console.log('student -> ',student,'studentCopy =>', studentCopy)
/* student->
name:"Sid"
class:"KG"
subject:"JS"
marks:"100"

studentCopy ->
name:"Alic"
class:"KG"
subject:"JS"
marks:"100"
*/
Enter fullscreen mode Exit fullscreen mode

What is Deep Copy?

Deep copy is when you copy a nested array/object to another object. Like object has object in it. See Below

const student = {
  name: 'Sid',
  class: 'KG',
  subject: 'JS',
  marks: '100',
  address:{
    street: '29b',
    city: 'honkong'
  }
}
console.log(student);
/* output ->
name:"Sid"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "honkong"
*/

const studentCopy = {...student}
console.log(studentCopy)
/* output ->
name:"Sid"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "honkong"
*/

studentCopy.name = 'Peter'
console.log('student -> ',student,'studentCopy =>', studentCopy)

/* student->
name:"Alic"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "New York"

studentCopy ->
name:"Peter"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "New York"
*/

studentCopy.address.city = 'New York'
console.log('student -> ',student,'studentCopy =>', studentCopy)

/* student->
name:"Alic"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "New York"

studentCopy ->
name:"Alic"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "New York"
*/

Enter fullscreen mode Exit fullscreen mode

There's nothing new in above three outputs.Nested objectβœ…, Copy of nested objectβœ…. Change the property of copy object will not affect the other βœ…. But as soon as you hit the forth block of code , you head gonna 🀯.

The question is why this behavior?
In nested objects the copy is created for parent object nested objects are assign by reference. You can change the value of parent object and get two different array but if you change the value of nested array you'll see the parent array's value also change. So the question arises...

How to resolve the deep copy issue ?

Use JSON.parse() and JSON.stringifty()

See code below

const studentCopy2 = (JSON.parse(JSON.stringify(student)));
studentCopy2.address.city = 'Warsaw';
console.log('student -> ',student,'studentCopy =>', studentCopy,'studentCopy2 =>', studentCopy2)

/* student->
name:"Alic"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "New York"

studentCopy ->
name:"Alic"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "New York"

studentCopy2 ->
name:"Alic"
class:"KG"
subject:"JS"
marks:"100"
address:
    street: "29b"
    city: "Warsaw"
*/
Enter fullscreen mode Exit fullscreen mode

Source
stackoverflow

medium

medium

dev.to

Thanks to @amanse
End.

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay