String.prototype.slice() extracts part of a string into a new string without changing the original (same API as MDN String.prototype.slice()). Learn start/end indexes, exclusive ends, negative indexes, how it compares to substring(), five examples, and try-it labs.
01
Kind
Instance method
02
Returns
New string
03
End index
Exclusive
04
Negatives
From the end
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
Need the middle of a sentence, the last few characters, or everything after an index? slice() copies that section into a new string.
Pass a start index, and optionally an end index. The end is exclusive — characters from start up to (but not including) end are kept. Negative numbers count backward from the end of the string.
💡
Beginner tip
slice(0, 3) keeps indexes 0, 1, and 2 — three characters. The character at index 3 is left out.
Examples follow MDN String.slice() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Extract from the middle, the end, and with negatives.
Example 1 — Basic slice()
MDN demo: pull pieces out of a classic pangram sentence.
JavaScript
const str = "The quick brown fox jumps over the lazy dog.";
str.slice(31);
// "the lazy dog."
str.slice(4, 19);
// "quick brown fox"
str.slice(-4);
// "dog."
str.slice(-9, -5);
// "lazy"
slice(4, -2) starts at index 4 and stops two characters before the end. Starting past the length returns an empty string.
📈 Practical Patterns
Negatives, edge cases, and everyday helpers.
Example 3 — Negative Indexes
MDN walkthrough: count backward from the end.
JavaScript
const str = "The morning is upon us.";
str.slice(-3); // "us."
str.slice(-3, -1); // "us"
str.slice(0, -1); // "The morning is upon us"
str.slice(4, -1); // "morning is upon us"
str.slice(-11, 16); // "is u"
str.slice(11, -7); // " is u"
Positive starts take a prefix. Negative starts take a suffix. Truncation keeps room for an ellipsis character.
Applications
🚀 Common Use Cases
Take a prefix / suffix — slice(0, n) or slice(-n).
Drop edges — slice(1, -1) to remove first and last characters.
Preview text — truncate long strings for UI cards.
Parse fixed formats — pull year/month pieces from known layouts.
Not for regex cuts — combine with search / indexOf when the start is dynamic.
Not for splitting lists — use split() for delimiter-based pieces.
🧠 How slice() Builds the Result
1
Read start / end
Default start to 0; default end to the string length.
Input
2
Normalize negatives
Add str.length to negative indexes (floored at 0).
Adjust
3
Copy the window
Copy characters from start up to (not including) end.
Extract
4
✅
Return a new string
Empty when the window is invalid; original str never changes.
Important
📝 Notes
The end index is exclusive.
Negative indexes count from the end of the string.
If start ≥ end after normalizing, the result is "" (no swap).
Prefer slice() over legacy substr().
Indexes are UTF-16 code-unit positions (same as length / charAt).
Always capture the return value — the original string is unchanged.
Compatibility
Browser & Runtime Support
String.prototype.slice() is Baseline Widely available — supported across modern browsers since July 2015 (and far earlier as a core language feature).
✓ Baseline · Widely available
String.slice()
Safe for production in browsers and runtimes (Node.js, Deno, Bun). Prefer slice() for extracting string sections in new code.
UniversalWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
slice()Excellent
Bottom line: Use String.slice() to copy a section of a string. Remember the end index is exclusive, and negatives count from the end.
Wrap Up
Conclusion
String.prototype.slice() is the modern, flexible way to extract part of a string — with exclusive ends and negative indexes that match how Array.prototype.slice works. Prefer it over substr(), and reach for substring() only when you need its swap behavior.
String.prototype.slice(indexStart, indexEnd?) returns a new string containing characters from indexStart up to (but not including) indexEnd. The original string is unchanged.
No. indexEnd is exclusive — the character at indexEnd is not included. For example "JavaScript".slice(4, 10) returns "Script" (indexes 4–9).
Negative values count from the end of the string. slice(-4) means “start 4 characters from the end.” slice(-9, -5) extracts that window from the end.
After negative indexes are normalized, if the end is before or at the start, slice() returns an empty string "".
slice() supports negative indexes. substring() treats negatives as 0 and swaps the two arguments if start > end. Prefer slice() in modern code.
No. Strings are immutable. slice() returns a new string.
Did you know?
String.slice and Array.slice share the same mental model: exclusive end indexes and negatives that count from the end. Once you learn one, the other feels familiar.