Rest parameters

2 snippets in JavaScript

JSJavaScript

Rest Parameters

JS · Variables & Constants
Syntax
function fn(...args) {}
Example
function sum(...numbers) {
  return numbers.reduce((total, n) => total + n, 0);
}
console.log(sum(5, 10, 15));
Output
30

Note Rest must be the last parameter. Unlike the old arguments object, rest gives you a real Array with all array methods.

Rest Parameters in Functions

JS · Functions
Syntax
function fn(first, ...rest) {}
Example
function logTagged(level, ...messages) {
  const timestamp = new Date().toISOString();
  console.log(`[${level}] ${timestamp}:`, ...messages);
}
logTagged("INFO", "Server started", "on port 3000");
Output
[INFO] 2026-04-04T...: Server started on port 3000

Note Rest parameters collect remaining arguments into a real Array. Unlike arguments, they work in arrow functions too.

Frequently asked questions

How does JavaScript handle rest parameters?
JavaScript covers this with 2 copy-ready snippets on this page. The "Rest Parameters" snippet in JavaScript uses `function fn(...args) {}`.
Which code does the JavaScript example use?
The "Rest Parameters" snippet uses `function fn(...args) {}`, from the Variables & Constants section of the JavaScript cheat sheet.
What other JavaScript snippets are shown for "rest parameters"?
Besides "Rest Parameters", this page also shows "Rest Parameters in Functions".
Is there anything to watch out for?
Yes. For "Rest Parameters": Rest must be the last parameter. Unlike the old arguments object, rest gives you a real Array with all array methods.