ECMAScript 2015 (ES6) Rest parameter – Quick reference

Rest parameters

The rest parameter syntax allows us to represent an indefinite number of arguments as an array with ... prefix.

Basic usage

In the above example, marks would collect the second argument of the function (because the first one is mapped to name) and all the consecutive arguments.

Difference between rest parameters and the arguments object

There are three main differences between rest parameters and the arguments object:

  • rest parameters are only the ones that haven’t been given a separate name, while the arguments object contains all arguments passed to the function;
  • the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
  • the arguments object has additional functionality specific to itself (like the callee property).

Example

Leave a Reply

Your email address will not be published. Required fields are marked *