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.