slice() method creates a new array from a portion of an existing array. Let's explore how this works.
Array.prototype.slice() method returns a shallow copy of a portion of an array into a new array object. It takes two parameters:
end (optional). The method doesn't modify the original array but instead returns a new one.
slice() method also accepts negative indexes. A negative index indicates an offset from the end of the array. For instance,
-1 refers to the last element,
-2 to the second last, and so on.
end parameter is omitted,
slice() will extract till the end of the array.
Array.prototype.slice() method is supported in all modern browsers, and IE6 and up.
Join our community of forward-thinkers and innovators. Subscribe to get the latest updates on courses, exclusive insights, and tips from industry experts directly to your inbox.