Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.
The index of the first character to include in the returned substring.
indexEnd OptionalThe index of the first character to exclude from the returned substring.
A new string containing the extracted section of the string.
slice() extracts the text from one string and returns a new string.
slice() extracts up to but not including indexEnd. For example, str.slice(4, 8) extracts the fifth character through the eighth character (characters indexed 4, 5, 6, and 7):
indexStart indexEnd ↓ ↓ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | T | h | e | | m | i | r | r | o | r | m i r r _______________ ↑ ResultThe following example uses slice() to create a new string.
The following example uses slice() with negative indexes.
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.
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.
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.
| ECMAScript® 2027 Language Specification # sec-string.prototype.slice |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 10, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.