Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

rest parameters

Rest parameters let a function accept any number of arguments by gathering the remaining arguments into an array. They are written with ... before the final parameter name.

How it works

A rest parameter is written as three dots before the final parameter name in a function declaration. When the function runs, any arguments not assigned to earlier named parameters are collected into that parameter as a real array, so array methods can be used directly.

Syntax

function name(...rest) { /* body */ }

Parameters

NameTypeDescription
argsparametersNamed parameters before the rest parameter receive their corresponding arguments.
...restarrayThe final rest parameter receives all remaining arguments in an array.

Returns

The page does not specify a general return value; the function's own return statement determines its result.

Key points

  • Arguments are gathered into one array rather than being exposed as separate named parameters.
  • Functions can receive more arguments than their explicitly named parameters; a rest parameter captures the extras.
  • Use rest parameters when designing variadic functions whose input count is not fixed.
  • The meaning of ... depends on its position: at the end of a function parameter list, it is rest syntax.

Common mistakes

  • A rest parameter must be the function's final parameter; placing another parameter after it is invalid.

Example: Collecting arguments with a rest parameter

function sum(...numbers) {
  return numbers.reduce((total, number) => total + number, 0);
}

console.log(sum(2, 4, 6)); // 12

The rest parameter gathers all arguments after the function's named parameters into an array.

When to use it

Variadic calculations

A function must accept a variable number of numeric inputs.

Argument forwarding

A wrapper forwards an arbitrary number of arguments to another function.

Flexible collection processing

A utility needs direct access to array methods while processing extra inputs.

  • spread syntax
  • arguments
  • function parameters

Source: Rest parameters and spread syntax. Checked 2026-10-15.