JavaScript (ES2026) reference · String Methods
JavaScript String slice() Method
Extract part of a string between two indexes; negative indexes count from the end.
JavaScript (ES2026) Since ES3
Syntax
string.slice(start, end)
Parameters
| Parameter | Description |
|---|---|
start |
Index to start at (negative counts from the end). |
end optional |
Index to stop before. Default: string.length |
Return value
A new string.
Examples
const s = "JavaScript";
console.log(s.slice(0, 4), s.slice(4), s.slice(-6), s.slice(-6, -3));
Output
Java Script Script Scr
const text = "A very long product description";
console.log(text.length > 15 ? text.slice(0, 15) + "…" : text);
Output
A very long pro…
Version history
| Version | Change |
|---|---|
| JavaScript ES3 | Added |
In other languages
slice()· Pythonsubstr()· PHPmb_substr()· PHP
Related
string.substring()· Return the part of a string between two indexes. Unlike slice(), negative values become 0 and swapped arguments are reordered.string.at()· Return the character at an index, where negative indexes count from the end: str.at(-1) is the last character.array.slice()· Return a shallow copy of part of the array, from start up to (not including) end. The original is not changed.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial