UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ String/ String.prototype.slice()

The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.

Syntax

slice(indexStart)
slice(indexStart, indexEnd)

Parameters

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. Changes to the text in one string do not affect the other string.

slice() extracts up to but not including indexEnd. For example, str.slice(1, 4) extracts the second character through the fourth character (characters indexed 1, 2, and 3).

Examples

Using slice() to create a new string

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); // ""

Using slice() with negative indexes

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"

Specifications

Browser compatibility

See also