| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
The slice() method of String values extracts a section of this string and
returns it as a new string, without modifying the original string.
const str = "The quick brown fox jumps over the lazy dog.";
console.log(str.slice(31));
// Expected output: "the lazy dog."
console.log(str.slice(4, 19));
// Expected output: "quick brown fox"
console.log(str.slice(-4));
// Expected output: "dog."
console.log(str.slice(-9, -5));
// Expected output: "lazy"
slice(indexStart)
slice(indexStart, indexEnd)
indexStartThe index of the first character to include in the returned substring.
indexEnd OptionalThe index of the first character to exclude from the returned substring.
A new string containing the extracted section of the string.
slice() extracts the text from one string and returns a new string.
slice() extracts up to but not including indexEnd. For example, str.slice(4, 8) extracts the fifth character through the eighth character (characters indexed 4, 5, 6, and 7):
indexStart indexEnd
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| T | h | e | | m | i | r | r | o | r |
m i r r
_______________
Result
indexStart >= str.length, an empty string is returned.indexStart < 0, the index is counted from the end of the string. More formally, in this case, the substring starts at max(indexStart + str.length, 0).indexStart is omitted, undefined, or cannot be converted to a number, it's treated as 0.indexEnd is omitted or undefined, or if indexEnd >= str.length, slice() extracts to the end of the string.indexEnd < 0, the index is counted from the end of the string. More formally, in this case, the substring ends at max(indexEnd + str.length, 0).indexEnd <= indexStart after normalizing negative values (i.e., indexEnd represents a character that's before indexStart), an empty string is returned.The following example uses slice() to create a new string.
const str1 = "The morning is upon us."; // The length of str1 is 23.
const str2 = str1.slice(1, 8);
const str3 = str1.slice(4, -2);
const str4 = str1.slice(12);
const str5 = str1.slice(30);
console.log(str2); // he morn
console.log(str3); // morning is upon u
console.log(str4); // is upon us.
console.log(str5); // ""
The following example uses slice() with negative indexes.
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'
This example counts backwards from the end of the string by 11 to find the
start index and forwards from the start of the string by 16 to find the end
index.
console.log(str.slice(-11, 16)); // "is u"
Here it counts forwards from the start by 11 to find the start index and
backwards from the end by 7 to find the end index.
console.log(str.slice(11, -7)); // " is u"
These arguments count backwards from the end by 5 to find the start index
and backwards from the end by 1 to find the end index.
console.log(str.slice(-5, -1)); // "n us"
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.slice |
This page was last modified on Jul 10, 2025 by MDN contributors.
Stringanchor()at()big()blink()bold()charAt()charCodeAt()codePointAt()concat()endsWith()fixed()fontcolor()fontsize()includes()indexOf()isWellFormed()italics()lastIndexOf()link()localeCompare()match()matchAll()normalize()padEnd()padStart()repeat()replace()replaceAll()search()slice()small()split()startsWith()strike()sub()substr()substring()sup()toLocaleLowerCase()toLocaleUpperCase()toLowerCase()toString()toUpperCase()toWellFormed()trim()trimEnd()trimStart()valueOf()[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |