JavaScript Array slice() method
returns a shallow copy of a portion of the array based on the start and end index passed

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);

syntax 1 description
return a shallow copy of myArray
myArray.slice()
syntax 2 description
return a new array from start index to end of myArray
myArray.slice(start)
syntax 3 description
return a new array from start index to end index of myArray (end not included)
myArray.slice(start, end)