String.slice() method is a way to extract part of a
string and return it as a new
string while leaving the original unchanged.
beginIndexis where the
stringwill be sliced.
endIndex(optional) is where the slice will stop. If no
endIndexis given, the slice will extract to the length of the string.
endIndexis specified the string will be sliced up until the specified index, but not including it.
Slicing a String
var str = "Welcome to Code Snippet." str.slice(11, -1) // returns "Code Snippet"
NOTE: Notice that the
.slice() method also works with a negative index.
In this case, using
-1 as the
endIndex removed the
. from the end of the string.
Shoot me an email at
firstname.lastname@example.org with any questions and if you enjoyed this, stay tuned and subscribe below! 👇