Copying objects with deep copy The JSON.stringify and JSON.parse methods can be used to deep copy an object. In a deep copy, the source and target objects have different memory addresses and are not connected at all. The JSON.stringify() method will take a JavaScript object as an argument and transform it into a string. Then the JSON.parse() method will parse the JavaScript string and return
Deep and shallow copying in JavaScript – part 2
Copying objects with Shallow copy Now that we went over shallow and deep copies in part 1, let’s go over shallow copying in JavaScript. There are 3 ways to create shallow copies in JavaScript: Iteration using for key in an object Spread operator Object.assign() 1. Iteration using for key in an object In order to copy the items of an object into another object we
Deep and shallow copying in JavaScript – part 1
Copying objects with shallow and deep copies Primitive values such as strings, and numbers are copied by value, whereas objects are copied by reference. What do we mean by this? The following code example will explain this practically: let num1 = 10; let num2 = num1; num1 = 6; console.log(num2); //10 The value of num1 which is the number 10 is assigned to the variable