The rest parameter

The rest parameter is also represented by the ... token. The last parameter of a function with ... is called a rest parameter. The rest parameter is an array type and contains the rest of the parameters of a function when the number of arguments exceeds the number of named parameters. The rest parameter is used to capture a variable number of function arguments from within a function. The arguments object can also be used to access all arguments passed. The argument object is not strictly an array, but it provides some interfaces that are similar to an array. The following example code shows how to use the arguments object to retrieve the extra arguments:

function myFunction(a, b) { const args = Array.prototype.slice.call(arguments, ...

Get Learn ECMAScript - Second Edition 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.