slice()

ES3+

Returns a shallow copy of a portion of an array into a new array object.

Syntax

array.slice(start, end)

Parameters

start number optional

Index at which to start extraction

end number optional

Index before which to end extraction

Return Value

Array

A new array containing the extracted elements

Examples

JavaScript
const fruits = ['apple', 'banana', 'cherry', 'date'];
console.log(fruits.slice(1, 3));
console.log(fruits.slice(-2));
Output:
// ['banana', 'cherry'] ['cherry', 'date']

Related Methods