Clone JavaScript Object with JSON.stringify

Advantages, Disadvantages of Cloning Object with JSON.stringify

Mayank Gupta
TechnoFunnel
Published in
3 min readSep 7, 2020

--

Deep Cloning Objects in JavaScript, Object Cloning with JSON.stringify, Cloning using JavaScript, Deep vs Shallow Copy
Object Cloning with JSON.stringify

TechnoFunnel presents another article on Object Deep Cloning with JSON.stringify in details. We will also discuss about advantages and disadvantages of cloning using JSON.stringify approach. Lets look for the simple implementation of JSON.stringify to clone an Object.

In the above code, we have a simple variable which is first converted to “string” format and saved inside a variable. This variable can then be parsed back to Object form using “JSON.parse

Once the string has been parsed, the Object is reconstructed out of the stringified Object. Given below is the output:

The Object created above “clonedObject”, is a separate object having a different memory block. The Object is not the reference of the initial object “employeeData”. Lets look into further details about Object cloning using this method.

Advantages of Cloning Object with JSON.stringify

  1. The Complex Objects are deep cloned using JSON.stringify
  2. The Object is Deep Cloned using this method.

Lets add another key “address” to the variable “employeeData”. This key is a complex type containing further “key-value” pairs. Given below is the complex Object that we need to clone next.

In the above example we need to ensure that the “address” property is copied by value rather than reference using…

--

--

Mayank Gupta
TechnoFunnel

9 Years of Experience with Front-end Technologies and MEAN Stack. Working on all Major UI Frameworks like React, Angular and Vue https://medium.com/technofunnel