JavaScript Array.prototype.slice() Method
The slice() method returns a copy of part of an array, from a start index up to an end index, without changing the original.
Description
The slice() method returns a shallow copy of a portion of an array, from start up to but not including end. The original array isn't changed. Negative indexes count back from the end, so slice(-2) gives the last two elements, and slice() with no arguments copies the whole array.
Don't confuse it with splice(), which removes or inserts elements in place.
Copying an array with slice() is shallow: objects inside are shared between the original and the copy. slice() is also commonly used to convert array-like objects, as in Array.prototype.slice.call(arguments) in older code, although Array.from() is the modern way. Strings and typed arrays have slice() methods that work the same way.
Syntax
array.slice()
array.slice(start)
array.slice(start, end)Parameters
- startOptional. The index to start at. Negative values count from the end. Defaults to 0.
- endOptional. The index to stop before. Negative values count from the end. Defaults to the length.
Return Value
A new array containing the extracted elements.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.slice() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





