slice() in JavaScript – How to Clone Array Elements
Whenever you use slice() on an array, the method does the following:
- It creates a new array.
- It duplicates a specified part of its calling array into the newly created array—without altering the original array.
Syntax of the slice()
Method
slice()
accepts two arguments. Here is the syntax:
Argument 1: startIndex
The startIndex
argument is the first argument accepted by the slice()
method. It defines the index position where you want the duplication to start.
Example: Slice an array from the second index position
In the snippet above, we used slice()
to duplicate all the colorsArray
’s items from index 2
.
Keep in mind that the startIndex
is part of the items that the computer will duplicate.
Example: Slice an array from the startIndex
position
You can see that "red"
is part of the items duplicated from colorsArray
.
"red"
got duplicated because slice()
starts copying an array from the startIndex
(0
in this case).
Example: Slice an array from the fourteenth index position
Suppose the startIndex
argument is greater than the calling array’s length. In that case, the computer will automatically change the startIndex
to the calling array’s length. As such, no item will get duplicated.
You can see that slice()
’s invocation returned an empty array because no item got duplicated from colorsArray
.
In other words, the system automatically changed colorsArray.slice(14)
to colorsArray.slice(2)
. And since no item is at the index position 2
, the computer returned an empty array.
Example: How to slice items from the end of an array
Suppose you wish to start duplicating items from the end of the calling array. In such a case, use a negative number as your startIndex
.
You can see that the index counting started from colorsArray
’s last item because we used a negative number as the startIndex
.
So, now that we know about the startIndex
parameter, we can discuss the endIndex
argument.
Argument 2: endIndex
The endIndex
argument is optional. It specifies the index position where you want slice()
’s duplication to end.
Example: Slice an array from the first to the fourth index position
In the snippet above, the colorsArray.slice(1, 4)
code instructs the computer to duplicate three of colorsArray
’s items—beginning from the first index position.
Suppose you omit the endIndex
argument or if its value is equal to or greater than callingArray.length - startIndex
(that is, the remaining items from the startIndex
). In such a case, the computer will duplicate all the array’s items—beginning from the startIndex
.
Example: Duplicate all array items from the first index position
The slice()
code in the snippet above duplicated all of colorsArray
’s items—from the first index position—because we omitted the endIndex
argument.
Example: Slice an array from the first to the thirty-fifth index position
The slice()
code in the snippet above duplicated all of the colorsArray
’s items—from the first index position—because the endIndex
argument is greater than callingArray.length - startIndex
(that is, 6 - 1
).
In other words, the endIndex
argument is greater than the number of remaining items from the startIndex
.
Example: Slice an array from the first to the second to the last index position
Suppose endIndex
is a negative number. In that case, the system will count the index positioning from the end of the calling array.
You can see that the computer counted endIndex
’s numbering from the end of the calling array because the endIndex
argument is negative.
Learn CSS Grid with Images
Important Stuff to Know about slice()
The slice()
method does a shallow copy of its calling array’s items.
So, suppose you used the method to copy an array’s object. In that case, slice()
will create a reference between the original object and the cloned one.
Therefore, whatever you change in the original array will reflect in the new one.
Here’s an example:
You can see in the snippet above that the colorsObject.laptop = "burlywood"
code updated colorsArray
and duplicatedColorsArray
.
The calling array and its duplicate got updated because slice()
did a shallow copy of colorsArray
. Therefore, whatever you change in the original will reflect in the new one.