Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Rest parameters

Collects the remaining function arguments into an array.

Available since: ES2015

Syntax

function name(...args) { … }

Examples

Any number of arguments:

const sum = (...numbers) => numbers.reduce((total, n) => total + n, 0);
 
console.log(sum(1, 2, 3));
console.log(sum());

Output:

6
0

The first argument separately, the rest into an array:

function tag(label, ...items) {
  return `${label}: ${items.join(', ')}`;
}
 
console.log(tag('Fruits', 'apple', 'pear'));

Output:

Fruits: apple, pear

Notes

The rest parameter must be the last one. It is a real array, unlike the old array-like arguments.

See also

In the tutorial

  • Parameters — Function parameters and arguments in JavaScript — several parameters, order, missing arguments and default values.

Thanks for reading!

Donate to the site — we work hard for you.