JavaScript String slice() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

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

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.

This page is part of JavaScript String Methods. Related topics include length and search().

Understanding the slice() Method

str.slice(indexStart, indexEnd) returns the characters from indexStart until indexEnd (exclusive).

  • Creates a new string; the original is unchanged.
  • indexEnd is optional — omit it to slice to the end.
  • Negative indexes count from the end of the string.
  • If start is past end (after normalizing), the result is "".
  • If start is beyond str.length, the result is "".

📝 Syntax

General forms of String.prototype.slice:

JavaScript
str.slice(indexStart)
str.slice(indexStart, indexEnd)

Parameters

  • indexStart — index of the first character to include. Negatives count from the end. Omitted / invalid values are treated as 0.
  • indexEnd (optional) — index of the first character to exclude. Omit to go to the end. Negatives count from the end.

Return value

A string with the extracted section:

SituationResult
Normal rangeCharacters from start up to (not including) end
End omittedFrom start to the end of the string
Negative indexesCounted from the end
Start ≥ lengthEmpty string ""
End ≤ start (after normalize)Empty string ""

Common patterns

JavaScript
"Mozilla".slice(1, 5);   // "ozil"
"Mozilla".slice(2);      // "zilla"
"Mozilla".slice(-3);     // "lla"
"Mozilla".slice(0, -1);  // "Mozill"
"Mozilla".slice(5, 1);   // ""

⚡ Quick Reference

GoalCode
From index to endstr.slice(i)
Between two indexesstr.slice(start, end)
Last n charactersstr.slice(-n)
Drop last characterstr.slice(0, -1)
First n charactersstr.slice(0, n)

🔍 At a Glance

Four facts to remember about String.slice().

Returns
string

Extracted section

End
exclusive

Not included

Negatives
from end

Like Array.slice

Mutates
no

Original stays intact

📋 slice() vs substring() vs substr()

slice()substring()substr()
Negative indexesCount from endTreated as 0Start from end (legacy)
If start > endEmpty stringArguments are swappedUses length argument
Second argumentEnd index (exclusive)End index (exclusive)Length to extract
RecommendationPrefer in new codeOK, but less flexibleAvoid (legacy)

Examples Gallery

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"
Try It Yourself

How It Works

slice(31) takes everything from index 31 onward. slice(4, 19) keeps indexes 4–18. Negative starts count from the end.

Example 2 — Create a New String

MDN demo: several slices of one sentence.

JavaScript
const str1 = "The morning is upon us."; // length 23

str1.slice(1, 8);   // "he morn"
str1.slice(4, -2);  // "morning is upon u"
str1.slice(12);     // "is upon us."
str1.slice(30);     // ""
Try It Yourself

How It Works

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"
Try It Yourself

How It Works

You can mix positive and negative ends. Negatives are converted using length before the cut is made.

Example 4 — Empty Results and Bounds

What happens at the edges of the string.

JavaScript
"Mozilla".slice(5, 1);   // ""  (start after end)
"Mozilla".slice(10);     // ""  (start past length)
"Mozilla".slice();       // "Mozilla" (start treated as 0)
"Mozilla".slice(0, 100); // "Mozilla" (end clamped to length)
Try It Yourself

How It Works

Unlike substring(), slice does not swap arguments when start > end — you get "". Oversized ends simply stop at the string’s end.

Example 5 — First / Last / Truncate

Handy helpers for previews and filenames.

JavaScript
function firstN(str, n) {
  return str.slice(0, n);
}

function lastN(str, n) {
  return str.slice(-n);
}

function truncate(str, max) {
  if (str.length <= max) return str;
  return str.slice(0, max - 1) + "…";
}

firstN("CodeToFun", 4);           // "Code"
lastN("report.pdf", 3);           // "pdf"
truncate("Learn JavaScript slice", 12);
// "Learn Java…"
Try It Yourself

How It Works

Positive starts take a prefix. Negative starts take a suffix. Truncation keeps room for an ellipsis character.

🚀 Common Use Cases

  • Take a prefix / suffixslice(0, n) or slice(-n).
  • Drop edgesslice(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.

📝 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.

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.

Universal Widely available
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
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.

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.

Continue with split(), search(), or the String methods hub.

💡 Best Practices

✅ Do

  • Use slice() for new substring extraction
  • Remember the end index is exclusive
  • Use negatives for suffixes: slice(-3)
  • Combine with indexOf / search for dynamic starts
  • Assign the returned string to a variable

❌ Don’t

  • Expect slice to swap start/end like substring
  • Use legacy substr() in new code
  • Forget emoji may use two UTF-16 code units
  • Mutate strings — always use the return value
  • Pass end < start and expect characters anyway

Key Takeaways

Knowledge Unlocked

Five things to remember about String.slice()

Extract a section into a new string.

5
Core concepts
✂️ 02

End

exclusive

Rule
🔙 03

Negatives

from end

Index
04

vs substring

no swap

Compare
📄 05

Mutates

no

Immutable

❓ Frequently Asked Questions

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.

More String Methods

Return to the hub for trim, replace, padStart, and more.

String methods hub →

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.

8 people found this page helpful