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

Rest Parameters Syntax

Learn the rest parameter syntax (...args) to collect remaining function arguments into an array.

Rest Parameters Syntax

What you'll learn: How to use the rest parameter syntax (...args) to collect all remaining function arguments into a single array.

Understanding Rest Parameters

When defining a function, you can use the rest parameter syntax to gather any number of arguments into an array. This is written as three dots (...) followed by a parameter name, and it must be the last parameter in your function signature.

Think of it like a catch-all basket: while named parameters grab specific items, the rest parameter scoops up everything else that remains.

Basic Syntax

function sum(first, ...numbers) {
  console.log(first);    // 1
  console.log(numbers);  // [2, 3, 4, 5]
}

sum(1, 2, 3, 4, 5);

Here, first captures the first argument, and ...numbers collects all remaining arguments into an array called numbers.

Rest Parameters Are True Arrays

Unlike the older arguments object, rest parameters give you a real array with all array methods available:

function multiply(...values) {
  return values.reduce((acc, val) => acc * val, 1);
}

multiply(2, 3, 4); // 24

Key Rules

  • Rest parameter must be last in the parameter list
  • There can only be one rest parameter per function
  • It collects remaining arguments (if any exist, it's an empty array otherwise)
function greet(greeting, ...names) {
  return `${greeting}, ${names.join(' and ')}!`;
}

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

Key Takeaway: Rest parameters (...paramName) let you capture unlimited function arguments as an array, making your functions flexible and giving you full array method access.