slice() in JavaScript – How to Clone String Elements
Whenever you use slice() on a string, the method does the following:
- It creates a new string.
- It duplicates a specified part of its calling string into the newly created string—without altering the original string.
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 a string from the second index position
In the snippet above, we used slice()
to duplicate all the colorsString
’s items from index 2
.
Keep in mind that the startIndex
is part of the items that the computer will duplicate.
Example: Slice a string from the startIndex
position
You can see that "r"
is part of the items duplicated from colorsString
.
"r"
got duplicated because slice()
starts copying a string from the startIndex
(0
in this case).
Example: Slice a string from the fourteenth index position
Suppose the startIndex
argument is greater than the calling string’s length. In that case, the computer will automatically change the startIndex
to the calling string’s length. As such, no item will get duplicated.
You can see that slice()
’s invocation returned an empty string because no item got duplicated from colorsString
.
In other words, the system automatically changed colorsString.slice(14)
to colorsString.slice(9)
. And since no item is at the index position 9
, the computer returned an empty string.
Example: How to slice items from the end of a string
Suppose you wish to start duplicating elements from the end of the calling string. In such a case, use a negative number as your startIndex
.
You can see that the index counting started from colorsString
’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 a string from the first to the fourth index position
In the snippet above, the colorsString.slice(1, 4)
code instructs the computer to duplicate three of colorsString
’s characters—beginning from the first index position.
Suppose you omit the endIndex
argument or if its value is equal to or greater than callingString.length - startIndex
(that is, the remaining items from the startIndex
). In such a case, the computer will duplicate all the string’s elements—beginning from the startIndex
.
Example: Duplicate all string elements from the first index position
The slice()
code in the snippet above duplicated all of colorsString
’s elements—from the first index position—because we omitted the endIndex
argument.
Example: Slice a string from the first to the hundredth index position
The slice()
code in the snippet above duplicated all of the colorsString
’s elements—from the first index position—because the endIndex
argument is greater than callingString.length - startIndex
(that is, 37 - 1
).
In other words, the endIndex
argument is greater than the number of remaining items from the startIndex
.
Example: Slice a string 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 string.
You can see that the computer counted endIndex
’s numbering from the end of the calling string because the endIndex
argument is negative.