.slice()

The .slice() array method returns a shallow copy of all or part of an array without modifying the original.

Syntax

The returned array contains the element specified by the first argument and all subsequent elements up to, but not including, the element specified by the second argument.

Loading...
  • start: The start index of the slice to be returned (optional)
  • end: The end index of the slice to be returned (optional)

If only one argument is specified, the returned array contains all elements from the start position to the end of the array.

Loading...

If start and end values are not provided, the slicing of array will be of the whole array.

Loading...

Two Arguments

To create a subarray of ['Tuesday', 'Wednesday', 'Thursday'] from weekDays array:

Loading...

Negative Arguments

A negative index can be used, indicating an offset from the end of the sequence. For example:

Loading...

To create the same subarray as the one above with negative index values:

Loading...

One Argument

To create a subarray of the last two elements from weekDays:

Loading...

Zero Arguments

To create an identical subarray of weekDays:

Loading...
Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn JavaScript on Codecademy