JavaScript String slice() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.slice(indexStart, indexEnd?) returns a new string with characters from indexStart up to (but not including) indexEnd. Negatives count from the end. Prefer slice() over deprecated substr().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.slice. Start with this line.

JavaScript
str.slice(start, end?)
CallWhat it does
str.slice(start, end?)Primary signature.
string.slice(...)Call with the arguments your case needs.

Built into JavaScript. Call slice(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.slice(start, end?)

Parameters

ParameterDescription
indexStartRequired. Where to begin (*omitted → treated as 0)
indexEndOptional. Stop before this index (default: end of string)

Return Value

Return ValueDescription
*The value returned by string.slice().

JavaScript Examples

Example 1 — Extract with Start and End

Pull a middle piece or everything from a start index onward.

JavaScript
const str = "JavaScript";

console.log(str.slice(0, 4)); // "Java"
console.log(str.slice(4));    // "Script"
console.log(str.slice(2, 6)); // "vaSc"
Try It Yourself

How It Works

  1. Set a window. slice(0, 4) keeps indexes 0–3 → "Java".
  2. Omit the end. slice(4) runs to the end of the string → "Script".
  3. Pick a middle range. slice(2, 6) keeps indexes 2–5 → "vaSc".
  4. Keep the original. str is still "JavaScript".

Example 2 — Negative Indexes

Count backward from the end for last characters or end-relative windows.

JavaScript
const str = "JavaScript";

console.log(str.slice(-6));      // "Script"
console.log(str.slice(-6, -3));  // "Scr"
console.log(str.slice(0, -6));   // "Java"
Try It Yourself

How It Works

  1. Convert from the end. Length is 10, so -6 becomes start index 4.
  2. Take a negative window. slice(-6, -3) is indexes 4 to 7 (exclusive) → "Scr".
  3. Trim from the end. slice(0, -6) stops six chars before the end → "Java".
  4. Mix freely. Positive and negative ends can be combined in one call.

Example 3 — slice() vs substring()

Same numbers can behave differently — especially with negatives.

JavaScript
const str = "JavaScript";

console.log(str.slice(-4));        // "ript"
console.log(str.substring(-4));    // "JavaScript" (neg → 0)
console.log(str.slice(4, 1));      // ""
console.log(str.substring(4, 1));  // "ava" (args swapped)
Try It Yourself

How It Works

  1. Negatives in slice. slice(-4) starts four from the end → "ript".
  2. Negatives in substring. substring(-4) treats -4 as 0 → whole string.
  3. No swap in slice. slice(4, 1) returns "".
  4. Swap in substring. substring(4, 1) becomes substring(1, 4) → "ava".

Browser & Runtime Support

String.prototype.slice() has been in JavaScript since the early language and is universal across browsers and runtimes.

✓ Baseline · Universal

String.prototype.slice()

Supported in all modern browsers, Internet Explorer, and every current Node.js release. Safe everywhere.

100% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.slice() Universal

Bottom line: Use confidently in any environment. Prefer slice() over substr() and reach for substring() only when you need its swap/negative-as-zero rules.

Frequently Asked Questions

String.prototype.slice(indexStart, indexEnd?) returns a new string with 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(0, 4) returns "Java" (indexes 0–3).
Negative values count from the end of the string. slice(-4) starts 4 characters from the end. You can mix positive and negative ends.
After negatives are normalized, if end is before or at start, slice() returns an empty string "". It does not swap the arguments.
slice() supports negative indexes. substring() treats negatives as 0 and swaps arguments when start > end. Prefer slice() in modern code.
No. Strings are immutable. slice() always returns a new string.

Next: small()

See the legacy HTML wrapper that makes text render smaller.

small() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful