JS: Function Rest Parameters

By Xah Lee. Date: . Last updated: .

New in JS2015.

Rest Parameters

Rest Parameters lets you define a function with arbitrary number of parameters, like this:

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

the name is received as a array.

// function with any number of parameters

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

console.log(
  JSON.stringify(ff(1, 2, 3, 4)) === "[1,2,3,4]",
);
// rest params as last arg
function ff(a, b, ...c) {
  return c;
}

console.log(
  JSON.stringify(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.log(JSON.stringify(((...xx) => xx)()) === "[]");

JavaScript, Function

BUY Ξ£JS JavaScript in Depth