Deep Copy and Shallow Copy in JavaScript

Bijay Gyawali
Jun 28, 2022

--

Deep Copy :

When we are creating a copy of variable, if we make changes in a new copy of a variable and it does not affect original value of variable.

Shallow Copy :

When we are creating a copy of variable, if we make changes in a new copy of a variable and it affect the original value of variable .

const person ={name : 'Harry',address : {country: 'nepal',city: 'butwal'}}// const updated ={name: person.name, address: {...person.address}}const updated = {       ...person,       address :{       ...person.address        }}updated.name = "Bijay"updated.address.city= 'kathamandu'console.log(person)console.log(updated)//person
{name : "Harry"
address
: {country: 'nepal', city: 'butwal'}
}
//updated
{name : "Bijay"
address
: {country: 'nepal', city: 'Kathmandu'}
}

--

--