Rest Operator

We will learn how to extract values from objects, arrays, and function arguments using the rest operator.

The rest operator is closely related to the spread operator. It takes care of the elements remaining in an array or object after destructuring or using function arguments. Its name is self-explanatory — it deals with the “rest” of the elements. Similar to the spread operator, the rest operator is preceded by three dots (). However, we cannot use it on the right side of an assignment and instead must always use it on the left… Moreover, only one rest operator can be used in each expression (in stark contrast to the spread operator).

Rest operator in function arguments

Let’s investigate the rest operator in function arguments. Assume that we want to write a function that can take any number of arguments. ES5 offers the keyword arguments, which lets us access an array of all the function arguments within our function:

Get hands-on with 1200+ tech skills courses.