How to create Shallow and Deep copy in JavaScript

1 min read

//create Object
let object = { name : "ravi", age : "36" }
// shallow copy
let ShallowCopy = object
//ShallowCopy
changes age 35 ShallowCopy.age=35
// print shallow copy and object reflect both object
console.log("ShallowcCopy : ", ShallowCopy) console.log("object : ", object)
//create deep object
let deepCopy = JSON.parse(JSON.stringify(object))
// change deepCopy object in age
deepCopy.age=34
// print deepcCopy object and object there is no change
console.log("deepcCopy : ", deepCopy)
console.log("object : ", object)
0
Subscribe to my newsletter
Read articles from Avinash Dewangan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
