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

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

  1. JS: Function Object
  2. JS: Function.prototype
Like what you read? Buy JavaScript in Depth