Rest Parameters — ES6 features-Part-2

Rest Parameter uses the same syntax (…) like Spread operator, but the use and functionality is little different.

We know that many JavaScript built-in functions support an arbitrary number of arguments. For instance:

  • Math.max(arg1, arg2, ..., argN) – returns the greatest of the arguments.
  • Object.assign(dest, src1, ..., srcN) – copies properties from src1..N into dest.

Here, we’ll try to do the same (make a function take any number of arguments). And also, how to pass arrays to such functions as parameters.

A function can be called with any number of arguments, no matter how it is defined. Like here:

JavaScript engine will not throw any error because of “excessive” arguments. But of course in the result only the first two will be counted.

The rest of the parameters can be included in the function definition by using three dots ... followed by the name of the array that will contain them. The dots literally mean “gather the remaining parameters into an array”.

For instance, to gather all arguments into array args:

Its same like, Math.min that computes the minimum of all the arguments it is given. If we want to write such function it will be some thing like this.

When such a function is called, the rest parameter is bound to an array containing all further arguments. If there are other parameters before it, their values aren’t part of that array. When, as in min, it is the only parameter, it will hold all arguments.

We can use a similar three-dot notation to call a function with an array of arguments.

This “spreads” out the array into the function call, passing its elements as separate arguments. It is possible to include an array like that along with other arguments, as in min(9, …numbers, -3). Square bracket array notation similarly allows the triple-dot operator to spread another array into the new array.

We can choose to get the first parameters as variables, and gather only the rest.

Here the first two arguments go into variables and the rest go into yourName array:

Note: The rest parameters must be at the end. The rest parameters gather all remaining arguments, so the following does not make sense and causes an error:

Destructuring Rest Parameters

Rest parameters can be destructured, but for arrays only. This means we can unpack rest parameters to variables.

Notice how the parameters are saved as variables. We can do the same inside the function too.

If we console.log the rest parameter with the dots, we get an array. But if we call it with three dots, we’re essentially unpacking the values into each separate number (not an array).

The “arguments” variable

There is also a special array-like object named arguments that contains all arguments by their index.

For instance:

In old times, rest parameters did not exist in the language, and using arguments was the only way to get all arguments of the function.

But the downside is that although arguments is both array-like and iterable, it’s not an array. It does not support array methods, so we can’t call for example.

Also, it always contains all arguments. We can’t capture them partially, like we did with rest parameters. So when we need these features, then rest parameters are preferred.

If we access the arguments object from an arrow function, it takes them from the outer “normal” function. Here’s an example:

As we remember, arrow functions don’t have their own this. Now we know they don’t have the special arguments object either.


We can summarize Spread Operator and Rest Parameter as:

When we see "..." in the code, it is either rest parameters or the spread syntax. There’s an easy way to distinguish between them:

  • When ... is at the end of function parameters, it’s “rest parameters” and gathers the rest of the list of arguments into an array.
  • When ... occurs in a function call or alike, it’s called a “spread syntax” and expands an array into a list.

Use patterns:

  • Rest parameters are used to create functions that accept any number of arguments.
  • The spread syntax is used to pass an array to functions that normally require a list of many arguments.

Together they help to travel between a list and an array of parameters with ease.

Front-end Web Developer, Bootcamp grad, learning Full-stack Web Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store