Sub topic 9
Passing Arrays as Arguments with Spread
This chapter assumes you know how to declare and call functions, how to work with arrays, and how rest parameters gather arguments into an array, which the previous chapter covered.
Rest parameters solve one half of a problem: a function that accepts any number of arguments. The other half comes up when your values already sit in an array and the function you want to call expects separate arguments. Spread syntax handles that case.
The Working Model
Spread syntax is ... placed before an expression inside a function call. It unpacks the array into individual arguments, in order.
function sum(first, ...rest) {
return rest.reduce((total, n) => total + n, first);
}
const numbers = [5, 10, 15];
console.log(sum(...numbers)); // 30, same as sum(5, 10, 15)
console.log(Math.max(...numbers)); // 15
console.log(Math.max(numbers)); // NaN, the whole array is one argumentThe two uses of ... mirror each other, and where the ... appears tells you which one you are looking at:
| Position | Name | Effect |
|---|---|---|
| In a parameter list | Rest | Gathers separate arguments into one array |
| In a call's argument list | Spread | Expands one array into separate arguments |
Mixing Spread with Other Arguments
You can place a spread anywhere in the argument list, and you can use more than one in the same call. The arguments are laid out left to right.
const low = [1, 2];
const high = [8, 9];
console.log(Math.max(0, ...low, 5, ...high)); // 9
// equivalent to Math.max(0, 1, 2, 5, 8, 9)Spread Works on Any Iterable
Spread accepts any iterable, meaning any value that can be looped over with for...of. Arrays, strings, Sets, and Maps all qualify. A plain object does not.
const unique = new Set([3, 7, 3, 1]);
console.log(Math.min(...unique)); // 1
function initials(a, b) {
return `${a}.${b}.`;
}
console.log(initials(..."JS")); // "J.S."
// Math.max(...{ a: 1 }); // TypeError: object is not iterableCommon Errors
- Forgetting the spread. Passing an array to
Math.maxgivesNaN, because the function receives a single array argument instead of numbers. - Spreading a non-iterable. Spreading
null,undefined, or a plain object inside a call throws aTypeError. Check the value first, or provide a fallback such as...(list ?? []). - Spreading huge arrays. Every element becomes a real argument, and engines cap how many arguments a call can take, usually somewhere in the tens of thousands or more. For very large arrays, use
reduceor a loop instead ofMath.max(...bigArray).
Exercise
Given const a = [4, 2] and function f(x, y, z) { return ${x}-${y}-${z}; }, what does f(...a, ...a) return, and why?
Show answer
It returns "4-2-4". The spread produces the four arguments 4, 2, 4, 2. Parameters x, y and z take the first three, and the extra fourth argument is ignored.
Exercise
Why does Math.max([3, 9]) return NaN while Math.max(...[3, 9]) returns 9?
Show answer
Without spread, Math.max receives one argument, the array itself, which converts to NaN when Math.max tries to treat it as a number. With spread, it receives the two separate numbers 3 and 9.