
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
In this lesson, you'll learn about cloning arrays and objects in JavaScript, which is essential for creating independent copies of data structures without maintaining references to the original.
When you want to create a copy of an array without keeping a reference to the original, you can use several methods. Let's explore these methods and understand how they work.
The spread operator is a modern addition to JavaScript that allows you to "spread" the values of arrays, objects, and strings. It's a concise way to clone arrays.
Imagine you have an array:
const numbers = [1, 2, 3, 4, 5];
const newNumbers = [...numbers];
The spread operator takes all the elements from the array and spreads them into a new array .
console.log(...numbers); // 1, 2, 3, 4, 5
console.log(newNumbers); // [1, 2, 3, 4, 5]
Let's compare the original and copied arrays:
const copiedNumbers = numbers;
console.log(numbers === copiedNumbers); // true
console.log(numbers === newNumbers); // false
numbers.push(6);
console.log(numbers); // [1, 2, 3, 4, 5, 6]
console.log(copiedNumbers); // [1, 2, 3, 4, 5, 6]
console.log(newNumbers); // [1, 2, 3, 4, 5]
The array remains unchanged, demonstrating that it is a "shallow clone".
The method can also be used to clone an array:
const numbers = [1, 2, 3, 4, 5];
const numbersCopy = numbers.slice();
console.log(numbersCopy); // [1, 2, 3, 4, 5]
Just like arrays, objects can be cloned using similar methods.
You can clone objects using the spread operator:
const person = {
name: 'Jon',
age: 20,
};
const otherPerson = { ...person };
// Modify the cloned object
otherPerson.age = 21;
console.log(person); // { name: 'Jon', age: 20 }
console.log(otherPerson); // { name: 'Jon', age: 21 }
The method can also be used to clone objects:
const A1 = { a: "2" };
const A2 = Object.assign({}, A1);
console.log(A2); // { a: "2" }
We've learned two different ways to clone both objects and arrays: using the spread operator and using or .
These methods create shallow clones meaning they copy the top-level properties but not nested objects.
In the next lesson, we'll explore the difference between shallow and deep clones and how to create a deep clone. Understanding these concepts is crucial for managing data effectively in JavaScript.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:00 Let's clone some arrays and objects.
00:00:02 In this case, I'll use the word clone instead of copy when we want to create independent copies of data structures without maintaining its reference to
00:00:11 the original.
00:00:12 So there are a couple of different ways in which we can do that.
00:00:15 Let me show you all of them one by one.
00:00:17 First, we'll talk about cloning arrays, and then we'll talk about cloning objects.
00:00:21 The first way to clone an array is by using something known as a spread operator.
00:00:27 It's a modern addition to JavaScript that allows you to spread the values of arrays, objects, and even strings.
00:00:34 And it is a super concise way to clone arrays.
00:00:38 Let me show you how it works.
00:00:39 Imagine you have an array called numbers.
00:00:42 and it just has numbers from 1 to 5. Now, you want to create new numbers, and you want those new numbers to be equal to numbers,
00:00:51 but you don't want to copy the reference as well.
00:00:53 Remember?
00:00:54 Just like this, we copy the same reference, right?
00:00:57 That's not good.
00:00:58 So instead, you have to find a way to somehow copy the values that are within that array.
00:01:05 And we can do that by first crafting a new array like this.
00:01:10 So now it gets its own reference.
00:01:13 This is abc123, and this one is 123abc, for example.
00:01:18 But now, how do we get those values?
00:01:20 Well, using a spread operator, we can say dot dot dot numbers.
00:01:26 This dot dot dot syntax will take all of the numbers from the original numbers array and spread them out within this new array.
00:01:34 So now if you console log the numbers, as well as the new numbers, you'll see that we get back the same array.
00:01:43 But now if you try to push a number six to the numbers array, this is the crucial part.
00:01:50 Check this out.
00:01:51 It gets added to the original array, but not to the one that we cloned.
00:01:56 Which means that using the spread operator, you can fully clone an array.
00:02:00 But if you do what we learned about before, const copied numbers and make it equal to numbers.
00:02:06 it'll have the same reference as the one at the top.
00:02:09 So, if we call this copied numbers, and we call this one cloned numbers, and now you console log all three ones, copied numbers,
00:02:20 numbers, and cloned numbers, you'll see that the first two, since they refer to the same reference, will get the number 6, but the cloned ones won't.
00:02:28 Similarly, if we try to compare the quality of numbers, to the copied numbers and numbers to the cloned numbers.
00:02:39 What do you think?
00:02:40 Well, check this out.
00:02:42 We get true, which means that numbers and copied numbers are the same, but numbers and cloned numbers don't point to the same reference.
00:02:50 The second way to clone an array is by using the array.slice method.
00:02:55 Once again, we can create the same numbers as before.
00:02:59 Make sure to comment these ones out.
00:03:01 And then you can see const.
00:03:04 cloned numbers will be equal to numbers.slice.
00:03:09 If you don't provide any parameters to the slice, it will copy the entire array, but this time by not creating a reference.
00:03:17 So now if we try it one more time by checking the equality, you'll see that the numbers and cloned numbers won't be the same.
00:03:25 But I just find using the spread operator much more intuitive.
00:03:28 You just say dot dot dot, place it here, and you get the values without the reference.
00:03:33 So, I prefer using the first way.
00:03:36 But now, let's talk about cloning objects, okay?
00:03:40 So what is the first way to clone an object?
00:03:43 Well, if you can guess it, it'll once again be the spread operator.
00:03:47 You can create a new person, something like name of John and an age of 30, and then you can create the other person, but this time,
00:03:58 just spread the properties of the original person.
00:04:02 If you do that and try to modify the original person's age to something like 31, and if you try to console log both the person as well as the other person,
00:04:15 you'll see that the original one we changed actually has age of 31, but the second one remained intact.
00:04:23 Also, we can verify with the equality check.
00:04:25 by checking whether person is triple equal to the other person, which in this case we get false because we have created a perfect clone.
00:04:33 And the second way to clone an object is by using the object.assign method.
00:04:41 How it works is you can create the same person object as before.
00:04:46 it'll look something like this, and then you can create another object, let's call it const anotherPerson, and make it equal to objectDataSign,
00:04:58 which you call, you provide the target, so which object you want to copy to, which you create as a new empty object, and then you specify an object you
00:05:08 want to copy it from, which will be the person.
00:05:11 So this is the same thing as above.
00:05:15 We take the values from the person and store it in a new object.
00:05:19 So once again, if you try to compare another person with the person, you'll get false because they are its own objects.
00:05:28 Once again, even in this case, I prefer using the spread operator because it is so intuitive.
00:05:35 You're simply saying, hey, give me the raw values that are in here and put them within this new object with the new reference that I just created.
00:05:42 Okay, so what have we learned?
00:05:45 Besides the fact that the spread operator is cool.
00:05:47 Well, we learned how to create something known as clones of complex values.
00:05:52 But let me make your life just a tiny bit more complicated.
00:05:56 What I taught you in this lesson is not the perfect way of creating a real deep clone.
00:06:03 What we're doing here is called a shallow clone, which means that you copy the top level properties, but not the nested objects,
00:06:12 which can lead to some more complications if your object has nested objects.
00:06:18 like a car right here that has a color of red.
00:06:22 If you spread the entire person, but then you try to change the color of the car of the original person to something like blue,
00:06:32 and then you try to console log the car value of the other person.
00:06:38 Check this out.
00:06:39 OtherPerson.car.color, what do you think?
00:06:43 Well, we created a clone, right?
00:06:44 It should be red because we copied the red car initially.
00:06:48 Well, check this out.
00:06:50 It is indeed blue.
00:06:51 So even though the reference to this original object, 123abc, has changed, right?
00:06:58 Because we created a new one that is abc123.
00:07:02 it still picked up the reference of this one, the inner objects within it.
00:07:09 So that's why this is called a shallow clone.
00:07:12 And don't get me wrong, in most cases, you will have just an object that has some properties within it.
00:07:18 I call it a simple object.
00:07:20 If that is the case and you know that you don't have any additional complex types within the object, go for it and simply spread it.
00:07:28 But if you're trying to create a clone of a more complex data type, you'll want to create something known as a deep clone and not a shallow clone.
00:07:37 So let me teach you how to do that in the next lesson.