JavaScript: 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 unspecifed number of parameters
// rest parameter.
function ff (a, b, ...c)
return c; // c is a array

); // [3, 4]

The syntax must be the last position.

The 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. JavaScript: Define Function
  2. JavaScript: Function Parameters
  3. JavaScript: Variable/Function Declaration Order, Name Hoisting
  4. JavaScript Variable Scope: Function Level vs Block Level
  5. JavaScript: Function Declaration vs Function Expression
  6. JavaScript: Closure
  7. JavaScript: Functional Programing
  8. JavaScript: Function Call, Apply, Bind
  9. JavaScript: Function Argument Default Value
  10. JavaScript: Function Rest Parameters
  11. JavaScript: Arrow Function

  1. JavaScript: Function Object
  2. JavaScript: Function.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.