JavaScript (ES2026) reference · String Methods

JavaScript String slice() Method

Extract part of a string between two indexes; negative indexes count from the end.

JavaScript (ES2026) Since ES3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.slice(start, end)

Parameters

ParameterDescription
start Index to start at (negative counts from the end).
end optional Index to stop before. Default: string.length

Return value

A new string.

Examples

Different rangesTry it yourself
const s = "JavaScript";
console.log(s.slice(0, 4), s.slice(4), s.slice(-6), s.slice(-6, -3));

Output

Java Script Script Scr
Truncate with an ellipsisTry it yourself
const text = "A very long product description";
console.log(text.length > 15 ? text.slice(0, 15) + "…" : text);

Output

A very long pro…

Version history

VersionChange
JavaScript ES3Added

In other languages

Was this page helpful?