Array methods

Array.prototype.slice()

Try the free JavaScript compiler

Copies a range shallowly, without mutating the receiver. End is excluded; negative indices are relative to length. Omitted end extends to length. Holes are preserved in ordinary sparse arrays.

Formal Syntax & Structure

javascript syntaxjavascript
array.slice(start, end)

Parameters & Attributes

NameTypeDescription
startNumberInclusive start; defaults to zero.
endNumberExclusive end; defaults to length.

Return Value

Array

Shallow selected range.

Working Example

Example Snippetjavascript
const a=[1,2,3];console.log(a.slice(-2).join(","),a.length);
JavaScript Reference