Shallow and deep copying in JavaScript
JavaScriptTranscript
English (Auto-generated)
Hi everyone in this cast we'll discuss a shallow and deep copies. So primitive values such as strings and numbers are copied by value whereas objects are copied by reference. So what do we mean by this? And let's take a look at an example. I'm going to declare a variable let number one is equal to the number 10. And then another variable called let number two whose value is num one. And then I'm going to reassign num one to the number, I'm going to assign the value 6 to the variable number one. Now the value of number one which is the number 10 is assigned to the variable number two. And if we console got a log number two we expect to see the number 10 in the console which you see down here. Now, even after we changed the value of number one to the number six the value of number two remains the same. It's because we copied by value of number one to number two before reassignment of the variable number 12 To the # six. So if I were to change the order of these statements and move this up here then out here in the console you'll see that the value of number two is now the integer six. No this is an example a simple example of copy by value. So what do we mean by copy by reference? Now when we copied by reference the memory address of an object is shared with another object. So let's take a look at an example. So I have a variable called lead number one and it has the amount key whose value is the number 10. And then I'm going to declare another variable called number two. And I'm going to assign a number one to be the value of the variable number two. And let me go ahead and change the value of the amount key in the number one object to sex. Now if I console dot log number two what do you think is going to be displayed in the console? And out here we see that we have the amount key in the number two object whose value is sex. So just like we did up here We are assigning the value of number one to be the value of number two. So we're assigning number one to number two. But then we changed the amount key to numb. We change the amount key in object number 1 to 6. And we console log number two we see that the value of the amount key is the integer six. Now Since the memory address of the two objects are the same. When we change the value of the number one amount key from the number 10 to sex we are changing the value at the memory location. Therefore the value of the mount key in the number two object will also be the number six. Since it refers to the same memory address. So out here if I were to move it up here like I did in the previous example we are still going to see the value of the mount key to be the integer six because out here the order doesn't matter. So let me go ahead and change this. It's because the memory address of these two objects are is the same now when we copy over objects we can't just simply use the assignment operator. Uh you know like we have out here, there are methods for copying objects and there are two ways of copying objects, shallow copying and deep copying. So let's go over briefly um what these two mean before we actually start making shallow and deep copies Now in a shallow copy, a new object is created which has the exact values as a source object, primitive values are copied by value. Whereas if a field is of a reference type, for example an array or another object, then the reference or memory address is copied over to the newly created target object. So in a shallow copy, the source object and the target object share the same memory address. Now in a deep copy all the values primitive and reference types are duplicated into a source object and allocated new memory locations. So in a deep copy, the memory address of the target and source objects is different. And changing the source object after a deep copy is not going to affect the target object. So now that we've briefly discussed, you know what shallow and deep copying means we can move over to a two strategies or methods of making shallow and deep copies. Now, an important note to keep in mind before we move on is that in a deep copy, the source and target objects have different memory addresses and are not connected. Whereas in a shallow copy, the source and target objects share in memory address. So how do we copy objects using both shallow and deep copying methods? And we'll go ahead and discuss that in the next cast.