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 optionalIndex at which to start extraction
end number optionalIndex 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']