Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.slice()

BaselineWidely available

Theslice() method ofString values extracts a section of this string andreturns it as a new string, without modifying the original string.

Try it

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"

Syntax

js
slice(indexStart)slice(indexStart, indexEnd)

Parameters

indexStart

The index of the first character to include in the returned substring.

indexEndOptional

The index of the first character to exclude from the returned substring.

Return value

A new string containing the extracted section of the string.

Description

slice() extracts the text from one string and returns a new string.

slice() extracts up to but not includingindexEnd. For example,str.slice(4, 8) extracts the fifth character through the eighth character (characters indexed4,5,6, and7):

              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
  • IfindexStart >= str.length, an empty string is returned.
  • IfindexStart < 0, the index is counted from the end of the string. More formally, in this case, the substring starts atmax(indexStart + str.length, 0).
  • IfindexStart is omitted,undefined, or cannot beconverted to a number, it's treated as0.
  • IfindexEnd is omitted orundefined, or ifindexEnd >= str.length,slice() extracts to the end of the string.
  • IfindexEnd < 0, the index is counted from the end of the string. More formally, in this case, the substring ends atmax(indexEnd + str.length, 0).
  • IfindexEnd <= indexStart after normalizing negative values (i.e.,indexEnd represents a character that's beforeindexStart), an empty string is returned.

Examples

Using slice() to create a new string

The following example usesslice() to create a new string.

js
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 mornconsole.log(str3); // morning is upon uconsole.log(str4); // is upon us.console.log(str5); // ""

Using slice() with negative indexes

The following example usesslice() with negative indexes.

js
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 by11 to find thestart index and forwards from the start of the string by16 to find the endindex.

js
console.log(str.slice(-11, 16)); // "is u"

Here it counts forwards from the start by11 to find the start index andbackwards from the end by7 to find the end index.

js
console.log(str.slice(11, -7)); // " is u"

These arguments count backwards from the end by5 to find the start indexand backwards from the end by1 to find the end index.

js
console.log(str.slice(-5, -1)); // "n us"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.slice

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp