JavaScript Array slice() method

JavaScript Array slice() method is used to slice an Array from given start index upto given end index.

Syntax

The syntax to call call() method on an array x is

where start=2, and end=5.

slice() method returns a new array, and the original array remains unmodified.

ADVERTISEMENT

Examples

In the following example, we take an array of numbers, and slice the array from index=2 upto index=5 using slice() method.

index.html

In the following example, we take an array of strings, and slice the array from index=1 upto index=5 using slice() method.

index.html

Conclusion

In this JavaScript Tutorial, we have learnt how to use slice() method to slice an Array and get sub-array.