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)()) === "[]");
JavaScript. Function
- JS: Define Function
- JS: Arrow Function
- JS: Function Parameters
- JS: the arguments object
- JS: Function Rest Parameters
- JS: Function Parameter Default Value
- JS: Function Argument Destructure (Pattern Matching)
- JS: Function. Declaration vs Expression
- JS: Closure
- JS: Function Call, Apply, Bind
- JS: Functional Programing
- JS: Function Pipe 📜
- JS: Function (class)
- JS: Function Constructor
- JS: Function.prototype