slice() method returns selected elements in an array, as a new array.
syntax ↴
slice(start, end) ↴
start index at which to start extraction. Default is 0.
end index at which to end extraction (not inclusive). Default is last element.
slice() method does not change the original array.
The start and end parameters specifies the part of the array to extract.
A negative number selects from the end of the array.
slice() method preserves empty slots ↴
const myArray = ['a', , 'b', , 'c'];
myArray.slice(0, 5); returns ↴
['a', empty, 'b', empty, 'c']
To remove first element from an array: myArray.slice(1);
To remove first and last element from an array: myArray.slice(1, -1);
myArray.slice()
myArray.slice(start)
myArray.slice(start, end)