Course contentsShow
← JavaScript
Lesson 982 of 2,72322. Spread and RestFree lesson

Rest Parameters Must Be Last

Explore the rule that rest parameters must appear at the end of the parameter list.

Rest Parameters Must Be Last

You'll learn why rest parameters must always come at the end of a function's parameter list and what happens if you try to put them elsewhere.

The Rule Explained

When you use rest parameters (...args) to collect "the rest" of the arguments into an array, JavaScript requires that this parameter be the last one in your parameter list. You cannot have any other parameters after it.

Think of it like a vacuum cleaner that sucks up everything remaining. Once you've gathered "everything else," there's nothing left to assign to subsequent parameters—so they're not allowed.

Valid Usage

function greet(greeting, ...names) {
  return `${greeting}, ${names.join(' and ')}!`;
}

greet('Hello', 'Alice', 'Bob', 'Charlie');
// "Hello, Alice and Bob and Charlie!"

Here, greeting gets the first argument, and ...names collects all the rest.

Invalid Usage

// ❌ SyntaxError: Rest parameter must be last formal parameter
function broken(...rest, last) {
  // Won't work!
}

// ❌ Also invalid
function alsoWrong(...rest, another, oneMore) {
  // Still won't work!
}

JavaScript will throw a syntax error before your code even runs because the engine cannot determine what to assign to parameters after the rest parameter.

Why This Makes Sense

The rest parameter's job is to collect everything that's left. If you could put parameters after it, JavaScript wouldn't know where to stop collecting for the rest parameter and where to start assigning to the next one.

Key Takeaway: Rest parameters must always be the last parameter in a function's parameter list—they gather everything remaining, leaving nothing for parameters that would follow.