JS: Function Rest Parameters

By Xah Lee. Date: . Last updated: .

New in ES2015.

Function's optional parameters can be specified by 3 dots followed by a name.

// function with any number of parameters
// rest parameter.
function ff (a, b, ...c)
return c; // c is received as array

); // [3, 4]

The ...rest syntax must be the last position.

The ...rest syntax can only happen once.

If no rest args are given, value is a empty array.

// rest params, if non given, value is empty array
function f1 (...r) { return r; };
console.log( f1() ); // []

ECMAScript 2015 §ECMAScript Language: Functions and Classes#sec-function-definitions

Function Topic

  1. JS: Define Function
  2. JS: Arrow Function
  3. JS: Function Parameters
  4. JS: var/function Declaration Order
  5. JS: var Name Scope
  6. JS: Function Declaration vs Function Expression
  7. JS: Closure
  8. JS: Functional Programing
  9. JS: Function Call, Apply, Bind
  10. JS: Function Argument Default Value
  11. JS: Function Rest Parameters
  12. JS: Function Argument Destructure

  1. JS: Function Object
  2. JS: Function.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon