

In other words, the returned array contains all elements from the starting to ending of the array. If only one parameter (index value) is specified, the slice() method creates a sequence of elements that begins from index and ends on the last array element. A new array that contains the sliced out or extracted elements. The slice() method returns a shallow copy of elements from the original array. The slice() method is used to convert an array-like object into an array.The slice() method of Array object comes into two flavors that are as: array.slice(index) The rgb array contains the first three elements of the colors array. Here is an example: var colors = Ĭonsole.log(rgb) // Code language: JavaScript ( javascript ) The typical use of the slice() method is to copy a portion of an array without modifying the source array. In this example, the newNumbers array contains all the elements of the numbers array. Var newNumbers = numbers.slice() Code language: JavaScript ( javascript ) The slice() is used to clone an array as shown in the following example: var numbers = In addition, it doesn’t change the source array. It’s important to keep in mind that the slice() method performs the shallow copy of elements to the new array only. The slice() returns a new array that contains the elements of the original array. If you omit the stop parameter, the slice() method will use the length of the array for the stop parameter.

It means that the slice() method doesn’t include the element at the stop position in the new array.


The slice() method extracts up to stop-1. The stop parameter, as its name implies, is a zero-based index at which to end extraction. If the start is undefined, slice() begins at 0. The start parameter determines the zero-based index at which to start extraction. The slice() method accepts two optional parameters as follows: slice(start, stop) īoth start and stop parameters are optional. Introduction to JavaScript Array slice() method In this tutorial, we will show you the practical uses of the JavaScript array slice() method. The Array.prototype object provides the slice() method that allows you to extract subset elements of an array and add them to the new array.
