String.prototype.slice()

Extracts a section of a string and returns it as a new string.

Since ES1 Spec ↗

Syntax

str.slice(start, end)

Parameters

NameTypeRequiredDescription
start number No Index where extraction begins. Negative counts from the end. Defaults to 0.
end number No Index before which to end extraction (exclusive). Negative counts from the end. Defaults to string length.

Returns

string — A new string containing the extracted section.

Examples

console.log('hello world'.slice(0, 5));
Output
hello
console.log('hello'.slice(-3));
Output
llo

Notes

Supports negative indices (unlike `substring`). Strings are immutable, so this returns a new string and never mutates the original.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also