Getting the head and tail of array using the rest operator

Using destructuring to pick out elements is convenient, but we don't always want to pull out every element. A commonly useful pattern is to get the zeroth element of an array assigned to one variable, and the rest of the elements in another. This is commonly called the head and tail of an array.

In this recipe, we'll take a look at how to use the rest operator to get the head and tail of an array.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.