Rest parameters (...args)
Collects remaining arguments into a real array.
function sum(...nums) { return nums.reduce((a,n)=>a+n,0); }This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Collects remaining arguments into a real array.
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array. Unlike the 'arguments' object, which is an array-like object but lacks built-in methods such as map, filter, or sort, rest parameters are actual Array instances. This leads to cleaner code and better performance by eliminating the need for manual conversions like Array.prototype.slice.call(arguments). A critical edge case is that rest parameters do not count toward the function's length property, which only reflects parameters preceding the '...'. In modern engines, rest parameters are optimized and preferred over 'arguments'. However, they must be the last parameter in the function signature, and a function can only contain one rest parameter. IE is not supported.
Quick reference
Syntax
function sum(...nums) { return nums.reduce((a,n)=>a+n,0); }
Inputs
Parameters
See it in practice
Examples
Basic Summation of Indefinite Arguments
function sumAll(...numbers) {
return numbers.reduce(function(acc, curr) {
return acc + curr;
}, 0);
}
console.log(sumAll(1, 2, 3, 4));10
Captures all passed arguments into a 'numbers' array and uses the reduce method to calculate the sum.
Combining Fixed and Rest Parameters
function multiplyAndMap(multiplier, ...values) {
return values.map(function(val) {
return val * multiplier;
});
}
var result = multiplyAndMap(2, 10, 20, 30);
console.log(result);[20, 40, 60]
The first argument is assigned to 'multiplier', and all subsequent arguments are gathered into the 'values' array.
Using Rest Parameters in Arrow Functions
var logger = (prefix, ...messages) => {
console.log(prefix + ': ' + messages.join(', '));
};
logger('DEBUG', 'User logged in', 'Session ID: 456');DEBUG: User logged in, Session ID: 456
Arrow functions do not have their own 'arguments' object, making rest parameters the standard way to handle variable arguments in them.
Debug faster
Common Errors
SyntaxError
Cause: Placing the rest parameter before other parameters in the function definition.
Fix: Always place the rest parameter as the very last item in the parameter list.
function wrong(first, ...rest, last) { /* logic */ }SyntaxError
Cause: Attempting to use more than one rest parameter in a single function signature.
Fix: Use a single rest parameter and destructure the resulting array if specific groupings are needed.
function multiRest(...group1, ...group2) { /* logic */ }Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Collects remaining arguments into a real array.
...args: All remaining arguments.
SyntaxError: Always place the rest parameter as the very last item in the parameter list. SyntaxError: Use a single rest parameter and destructure the resulting array if specific groupings are needed.