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 Arrow Function and function (keyword).

// function with any number of parameters

const ff = (...x) => {
 return x;
};

console.log(ff(1, 2, 3, 4));
// [ 1, 2, 3, 4 ]
// 2 parameters plus a rest param
function ff(a, b, ...c) {
 return c;
}
console.log(ff(1, 2, 3, 4));
// [ 3, 4 ]

if no arg given, rest params value is empty array.

// if no arg given, rest params value is empty array
console.log(((...xx) => xx)());
// []