String.prototype.slice()
Extracts a section of a string into a new string, accepting negative indexes that count from the end.
slice(indexStart)
slice(indexStart, indexEnd)- Added in JavaScript ECMAScript 3
Parameters
| Parameter | Type | Description |
|---|---|---|
$indexStart | number | The index of the first character to include. A negative value counts from the end, as max(indexStart + str.length, 0). |
$indexEndoptional | number= str.length | The index of the first character to exclude. Negative values count from the end the same way. Omitted or undefined extracts through the end of the string. |
Return value
stringA new string containing the extracted section, or an empty string when the range is empty.
slice() is the substring method to reach for by default. It returns a new string — the original is untouched — and it is the only one of the three extraction methods that understands negative indexes.
slice vs substring vs substr
| Case | slice | substring |
|---|---|---|
| Negative index | counts from the end | treated as 0 |
| End before start | returns "" | swaps the arguments |
That silent argument swap is why substring(4, 1) returns "bcd" while slice(4, 1) returns "". A bug that produces a reversed range shows up immediately with slice() and hides with substring(). substr() takes a length rather than an end index and is a deprecated legacy feature.
Common patterns
s.slice(-4)— the last four characters.s.slice(0, -1)— everything except the last character, the usual way to drop a trailing separator.s.slice(i + 1)— the tail after a delimiter found withindexOf().
Indexes are UTF-16 code units, so slicing in the middle of a surrogate pair leaves a broken half-character. Out-of-range values are clamped rather than throwing.
Example
When to use it
- Pull the year, month and day out of an ISO date string
- Drop a trailing slash or comma with slice(0, -1)
- Take the tail of a string after a delimiter located by indexOf()
- Truncate a preview to a fixed number of characters
Frequently asked questions
What is the difference between slice() and substring()?
slice() supports negative indexes and returns an empty string when the end is before the start; substring() treats negative values as 0 and silently swaps reversed arguments.
How do I get the last N characters of a string?
Use str.slice(-n). Guard against n being 0, because slice(-0) is slice(0) and returns the whole string rather than an empty one.
Does slice() modify the original string?
No. Strings are immutable, so slice() returns a new string and leaves the original unchanged. Assign the result to keep it.
More examples
slice() vs substring() on a reversed range
substring() quietly repairs a reversed range and ignores negative indexes, so a bad computed index produces plausible-looking wrong output. slice() returns an empty string and the bug surfaces.
const s = "abcdef";
console.log(JSON.stringify(s.slice(4, 1))); // "" — empty, as asked
console.log(JSON.stringify(s.substring(4, 1))); // "bcd" — arguments swapped
console.log(JSON.stringify(s.slice(-2))); // "ef"
console.log(JSON.stringify(s.substring(-2))); // "abcdef" — negative became 0
console.log(JSON.stringify(s.slice(0, -0))); // "" — -0 is 0, not the endTruncating without breaking an emoji
Cutting at code-unit index 9 lands inside the rocket's surrogate pair and leaves an unpaired half. Spreading to an array of code points first keeps every character whole.
const text = "Ship it \u{1F680} today";
const bad = text.slice(0, 9);
console.log(bad, bad.length); // trailing half of the rocket
const good = [...text].slice(0, 9).join("");
console.log(good); // "Ship it 🚀"
console.log(text.slice(0, -1)); // drop the last code unit
console.log([...text].slice(0, -1).join("")); // drop the last character
Discussion