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 Arrow Function and function (keyword).
// function with any number of parameters const ff = (...x) => { return x; }; console.log(ff(1, 2, 3, 4)); // [ 1, 2, 3, 4 ]
// 2 parameters plus a rest param function ff(a, b, ...c) { return c; } console.log(ff(1, 2, 3, 4)); // [ 3, 4 ]
if no arg given, rest params value is empty array.
// if no arg given, rest params value is empty array console.log(((...xx) => xx)()); // []