Learn JavaScript
Advanced Array Methods
31.3
Lesson

slice()

The slice() method copies a portion of an array into a new array.

In the example given above, the slice() method copies all the items starting from index 1 to 4, but not including index 4.

If we apply negative index values, the slice() method starts counting the position from the array end.

In the example given above, slice(-3) copies the last three elements in the array.

If we call slice() without any arguments, it creates a copy of the whole array.

If the starting index is greater than the number of items, it returns an empty array:

As can be seen in the examples given above, slice leaves the original array untouched.