Next: , Up: Default---Rest---Spread   [Index]


4.6.1 Rest Parameter Syntax

Rest parameters have been introduced to reduce the boilerplate code that was induced by the ‘arguments’.

The rest parameter syntax allows one to represent an indefinite number of arguments as an array. A function’s last parameter can be prefixed with ‘...’ which will cause all remaining (user supplied) arguments to be placed within a "standard" JavaScript array.

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

  1. 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;
  2. The ‘arguments’ object has additional functionality specific to itself (like the ‘callee’ property).
  3. The ‘...restParam’ bundles all the extra parameters into a single array, therefore it does not contain any named argument defined before the ‘...restParam’, whereas the arguments object contains all of the parameters — including all of the stuff in the ‘...restParam’ — unbundled.