Skip to content

rest parameters

Rest parameters are a function’s final parameter written with a leading ..., which collects every argument left over after the named parameters into a real array.

A rest parameter lets one function accept any number of arguments without a fixed parameter list. The collected values form an Array, so methods such as .map() and .join() work on them directly. A call with no extra arguments gets an empty array.

Rest parameters look like spread syntax but do the opposite job. Spread expands an iterable into separate values, and a rest parameter gathers separate arguments into one array. They also work in arrow functions, which have no arguments object of their own.

A function can have only one rest parameter, and it must come last, with no default value and no trailing comma. It doesn’t count toward the function’s .length. A function with a rest parameter also can’t contain a "use strict" directive in its body.

Example

Say a logging helper takes a severity level followed by any number of message parts:

Language: JavaScript
> const log = (level, ...parts) => `[${level}] ${parts.join(" ")}`;
undefined

> log("info", "Saved", 3, "files")
'[info] Saved 3 files'
> log("warn")
'[warn] '

> log.length
1

The first argument fills level, and the rest parameter parts collects the remaining three into an array. The call with only a level leaves parts as an empty array, so .join() still runs. The .length property counts only level, because a rest parameter isn’t part of a function’s expected arguments.

A rest parameter in any position other than last is a syntax error:

Language: JavaScript
> function tag(...labels, color) {}
function tag(...labels, color) {}
                      ^

Uncaught SyntaxError: Rest parameter must be last formal parameter
A developer pours shapes into a hopper feeding nested domes that hold a counter labeled CLOSURE, while capsules roll out under a bell and a clock.

Course

JavaScript Scope, Closures, and Functional Patterns for Pythonistas

Port a bouldering gym's front-desk script to JavaScript, and learn where names live and when they exist, how closures keep state, and how to work with rest parameters, callbacks, and timers.

intermediate js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 17, 2026