Functions
Rest parameters ...args
A function that takes any number of arguments: the extras are gathered into an array.
Why it exists
Some functions can't know in advance how many arguments they'll get: Math.max(3, 9, 2), console.log(a, b, c), a logger with any number of messages. You could make callers wrap everything in an array, but that's awkward and unusual. A rest parameter lets a function accept arguments as they are and see them inside as an array — with all array methods.
Before ES2015 there was the arguments object for this — an "almost array" without array methods, which arrow functions don't even get. Rest parameters replaced it.
Where it's used
- Utilities taking any number of values:
sum(...nums),max(...values),merge(...objects). - Loggers:
log(level, ...messages)— the first argument on its own, the rest as a list. - Wrappers that pass arguments along:
(...args) => fn(...args)indebounce,memoize,once. - Events:
emit(eventName, ...payload)hands subscribers everything after the event name.
Syntax
function name(first, ...others) {
// others is an array
}
const f = (...args) => args.length;Examples
function sum(...nums) {
let total = 0;
for (const n of nums) total += n;
return total;
}
sum(1, 2, 3); // → 6function log(level, ...messages) {
return `[${level}] ${messages.join(" ")}`;
}
log("WARN", "low", "on", "fish"); // → "[WARN] low on fish"const count = (...items) => items.length;
count(); // → 0
count("a", "b"); // → 2function modern(...args) {
return Array.isArray(args);
}
function old() {
return Array.isArray(arguments);
}
modern(1); // → true
old(1); // → falsearguments isn'tResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
On a call, parameters are filled left to right. Ordinary parameters take their arguments by position, and the rest parameter gets a new array of whatever is left. If nothing is left, it's an empty array [] — not undefined.
A rest parameter must be the last one, and there can be only one: function f(...a, b) is a syntax error, since it's unclear where the "rest" ends.
It mirrors spread: f(...arr) scatters an array into arguments, function f(...args) gathers them back. That's why the wrapper (...args) => fn(...args) passes arguments through unchanged, however many there are.
A rest parameter isn't counted in fn.length — the property that reports how many "required" parameters a function has.
Traps
How it differs from…
- Spread
... The same ... sign, in the opposite direction: spread in a call scatters an array into arguments, rest in parameters gathers them into an array.
- Default parameters
A default fills in one missing parameter; rest gathers any number of arguments. A rest parameter can't have a default — with no arguments it's already
[].
When to pick something else
If the arguments mean different things (name, age, city), don't gather them into a nameless array — give them names or take an options object. And if the caller already holds the data in an array, accepting the array is simpler than forcing it to spread with ....