JS By Example: 8 Distinct Uses Of JavaScript Array Slice

The JavaScript array slice method is one of the most powerful and commonly used built-ins in the JavaScript language.

And with the rise of React and other functionally oriented JavaScript practices, it is becoming even more important, for 2 key reasons:

Functional programming, particularly higher level functions, works heavily with lists of data
Functional programming requires pure functions, functions that do not cause side effects or modify their input data.
The JavaScript array slice method fits both of these criteria.

The slice method provides a mechanism for creating a shallow copy of a subset of a list, without modifying the original list. Thus it provides a key building block for writing functional JavaScript.

In this post we’ll master the slice method by example, exploring 8 different ways it can be used.

Caution: The slice method is not to be confused with the splice method, which modifies an array in place.

Slice leaves the original array intact and returns a shallow copy of selected items, splice modifies the original array.