Web/JavaScript/Reference/Global objects/String/slice
The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
str.slice(beginIndex[, endIndex])
Parameters
beginIndexThe zero-based index at which to begin extraction. If negative, it is treated as
str.length + beginIndex. (For example, ifbeginIndexis-3, it is treated asstr.length - 3.) IfbeginIndexis not a number afterNumber(beginIndex), it is treated as0.If
beginIndexis greater than or equal tostr.length, an empty string is returned.endIndexOptionalThe zero-based index before which to end extraction. The character at this index will not be included.
If
endIndexis omitted or undefined, or greater thanstr.length,slice()extracts to the end of the string. If negative, it is treated asstr.length + endIndex. (For example, ifendIndexis-3, it is treated asstr.length - 3.) If it is not undefined and not a number afterNumber(endIndex), an empty string is returned.If
endIndexis specified andstartIndexis negative,endIndexshould be negative, otherwise an empty string is returned. (For example,slice(-3, 0)returns"".)If
endIndexis specified, andstartIndexandendIndexare both positive or negative,endIndexshould be greater thanstartIndex, otherwise an empty string is returned. (For example,slice(-1, -3)orslice(3, 1)returns"".)
Return value
A new string containing the extracted section of the string.
Description
slice() extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.
slice() extracts up to but not including endIndex. str.slice(1, 4) extracts the second character through the fourth character (characters indexed 1, 2, and 3).
As an example, str.slice(2, -1) extracts the third character through the second to last character in the string.
Examples
Using slice() to create a new string
The following example uses slice() to create a new string.
let str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30);
console.log(str2) // OUTPUT: he morn
console.log(str3) // OUTPUT: morning is upon u
console.log(str4) // OUTPUT: is upon us.
console.log(str5) // OUTPUT: ""
Using slice() with negative indexes
The following example uses slice() with negative indexes.
let str = 'The morning is upon us.'
str.slice(-3) // returns 'us.'
str.slice(-3, -1) // returns 'us'
str.slice(0, -1) // returns 'The morning is upon us'
This example counts backwards from the end of the string by 11 to find the start index and forwards from the start of the string by 16 to find the end index.
console.log(str.slice(-11, 16)) // => "is u"
Here it counts forwards from the start by 11 to find the start index and backwards from the end by 7 to find the end index.
console.log(str.slice(11, -7)) // => " is u"
These arguments count backwards from the end by 5 to find the start index and backwards from the end by 1 to find the end index.
console.log(str.slice(-5, -1)) // => "n us"
Specifications
| Specification |
|---|
| ECMAScript (ECMA-262)The definition of 'String.prototype.slice' in that specification. |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
slice
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 4 |
Opera
Full support 4 |
Safari
Full support 1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
nodejs
Full support 0.1.100 |
Legend
- Full support
- Full support
See also
String.prototype.slice() by Mozilla Contributors is licensed under CC-BY-SA 2.5.