JS: Function Rest Parameters
(new in ECMAScript 2015)
Rest Parameters
Rest Parameters lets you define a function with arbitrary number of parameters. Like this:
(...name)
the name is received as a array.
- Rest Parameter must be the last in the parameter declaration.
- Space after the dots is optional.
- Rest Parameter can only happen once.
it works with keyword function
and also
Arrow Function
.
// function with any number of parameters function ff(...xx) { return xx; } console.log(ff(1, 2, 3, 4)); // [ 1, 2, 3, 4 ]
// rest params as last arg function ff(a, b, ...c) { return c; } console.log(ff(1, 2, 3, 4)); // [ 3, 4 ]
If no rest parameter is given, value is a empty array.
// rest params, if no arg given, value is empty array console.assert(JSON.stringify(((...xx) => xx)()) === "[]");