Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.slice

Returns part of a string.

Available since: ES3

Syntax

str.slice(indexStart, indexEnd)

Parameters

ParameterTypeDescription
indexStartnumberPosition of the first character, counting from zero. A negative value counts from the end of the string.
indexEndnumberOptional. The position to stop before. Defaults to the end of the string.

Return value

stringA new string with the selected characters.

Examples

The first three characters:

console.log('Programme'.slice(0, 3));

Output:

Pro

Negative values count from the end:

console.log('Programme'.slice(-5));
console.log('Programme'.slice(0, -1));

Output:

ramme
Programm

Notes

The original string is not modified. The character at indexEnd is not included in the result.

See also

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.