JS: Function Rest Parameters

By Xah Lee. Date: . Last updated: .

(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.

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