Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

slice

String.prototype.slice() returns a selected section of a string as a new string. The start is inclusive, the end is exclusive, and the original string remains unchanged.

How it works

Call slice() on a string with a starting index and, optionally, an exclusive ending index. Negative indexes count backward from the end; the method returns a new string and does not change the original.

Syntax

slice(indexStart)
slice(indexStart, indexEnd)

Parameters

NameTypeDescription
indexStartintegerThe zero-based index of the first character included in the result. Negative values count backward from the string's end; omitted or nonnumeric values act as 0.
indexEndintegerOptional zero-based index of the first character excluded from the result. Negative values count backward from the string's end; omitted or sufficiently large values select through the end.

Returns

A new string containing the extracted section of the string.

Key points

  • It creates a new string containing the selected section.
  • Calling the method leaves the source string unchanged.
  • indexStart identifies the first included character.
  • indexEnd is exclusive rather than inclusive.
  • An omitted, undefined, or sufficiently large end value selects the remainder.

Common mistakes

  • When the normalized end index is at or before the normalized start index, the result is an empty string.
  • A start index at or beyond the string length produces an empty string.
  • Negative start indexes count from the string's end, but values beyond the beginning are clamped to zero.

Example: Extract part of a string

const text = "JavaScript";
const part = text.slice(4, 10);
console.log(part); // "Script"

Extracts characters at indexes 4 through 9 and leaves the original string unchanged.

When to use it

Select a substring

Extracting a known range from text

Trim by position

Removing a prefix or suffix for display

Use negative indexes

Reading the final characters of a value

Interview questions

QWhat does String.prototype.slice() return, and does it mutate the source string?

String.prototype.slice() returns the selected portion as a new string. The start is included, while the end is excluded, and the original string is not modified.

Follow-up: What does slice(2, -1) do? It starts at index 2 and stops before the final character after normalizing the negative end index.

QHow are the start and end parameters interpreted?

The first parameter is the index of the first character to include. The optional second parameter is the index of the first character to exclude; omitting it extracts through the end.

Follow-up: What happens when the end index is less than or equal to the start index? The method returns an empty string.

QHow does slice() handle negative indexes?

A negative index is measured from the end of the string. If the start is negative, the effective start is the string length plus that value, with a lower bound of zero.

Follow-up: What happens when the start is greater than or equal to the string length? The result is an empty string.

  • String.prototype.substring()
  • String.prototype.substr()
  • Array.prototype.slice()

Source: String.prototype.slice() - JavaScript | MDN. Checked 2026-10-15.