JS: Function Rest Parameters
New in JS2015.
Rest Parameters
Rest Parameters lets you define a function with arbitrary number of parameters, like this:
function ff (...name) { return 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.
// function with any number of parameters function ff(...xx) { return xx; } console.log( JSON.stringify(ff(1, 2, 3, 4)) === "[1,2,3,4]", );
// rest params as last arg function ff(a, b, ...c) { return c; } console.log( JSON.stringify(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.log(JSON.stringify(((...xx) => xx)()) === "[]");
JavaScript, Function
- JS: Define Function
- JS: Arrow Function
- JS: Function Parameters
- JS: arguments Object
- JS: Function Rest Parameters
- JS: Function Argument Default Value
- JS: Function Argument Destructure
- JS: Function Declaration vs Function Expression
- JS: Closure
- JS: Function Call, Apply, Bind
- JS: Functional Programing
- JS: Function Pipe π
- JS: Function Object
- JS: Function Constructor
- JS: Function.prototype