JavaScript String.prototype.slice() Method
The slice() method extracts part of a string between two indexes and returns it as a new string. Negative indexes count from the end.
Description
The slice() method extracts a section of a string, from start up to but not including end, and returns it as a new string. Negative values count from the end, so slice(-3) gives the last three characters. It's generally preferred over substring() because of that negative-index support.
If start is greater than or equal to end, the result is an empty string; unlike substring(), the arguments are never swapped. Indexes count UTF-16 code units, so slicing in the middle of an emoji can leave half a character behind.
Common patterns include str.slice(0, n) to take the first n characters, str.slice(n) to drop them, and combining it with indexOf() to cut a string at a separator. Arrays and typed arrays have slice() methods with the same behavior.
Syntax
string.slice(start)
string.slice(start, end)Parameters
- startThe index to start at. Negative values count from the end.
- endOptional. The index to stop before. Negative values count from the end.
Return Value
A new string containing the extracted part.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.slice() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





