JS: Function Rest Parameters

By Xah Lee. Date: . Last updated: .

New in ES2015.

A function can have unspecified number of parameters, like this:

function ff (...name) { return name; }

That is, 3 dots followed by a name. In function body, the name is received as a array.

This is called “rest parameters”.

The syntax ...name must be the last in the parameter declaration. Space after the dots is optional.

The syntax ...name can only happen once.

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

console.log(
    ff(1,2,3,4)
); // [3, 4]

If no rest parameter is 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

JS Function

  1. Define Function
  2. Arrow Function ⭐
  3. Function Params
  4. “arguments” Object
  5. Rest Params ⭐
  6. Arg Default Value ⭐
  7. Arg Destructure ⭐
  8. f Declaration vs Expression
  9. Closure
  10. f call apply bind
  11. Functional Programing
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog