Sub topic 7
Spreading Arrays into Literals and Calls
This chapter assumes you can declare variables with let and const, work with arrays, and call functions. It also builds on the previous chapter, where ... in a parameter list collected arguments into an array. Here the same three dots do the opposite job. They expand an array into separate values.
Rest Collects, Spread Expands
The meaning of ... depends on where it appears:
| Position | Name | Effect |
|---|---|---|
| In a parameter list or a destructuring pattern | Rest | Gathers many values into one array |
| In an array literal or a function call's arguments | Spread | Unpacks one array into many values |
A quick way to tell them apart: rest sits on the receiving side, where values are assigned to it, and spread sits on the giving side, where values are produced from it.
Spread in Function Calls
Many functions take separate arguments rather than an array. Math.max is a common example. Spread lets you pass an array to it directly:
const values = [3, 9, 4];
console.log(Math.max(values)); // NaN (the whole array is one argument)
console.log(Math.max(...values)); // 9
// Spread can mix with ordinary arguments, in any position
console.log(Math.max(1, ...values, 20)); // 20Before spread existed, this needed Math.max.apply(null, values). Spread does the same thing and is easier to read.
You can also spread more than one array in a single call:
const a = [1, 5];
const b = [8, 2];
console.log(Math.min(...a, ...b)); // 1Spread in Array Literals
Inside square brackets, spread inserts each element at that spot. This gives you a short syntax for copying, joining, and inserting:
const values = [3, 9, 4];
const copy = [...values]; // [3, 9, 4], a new array
const wrapped = [0, ...values, 10]; // [0, 3, 9, 4, 10]
const joined = [...values, ...[7, 8]]; // [3, 9, 4, 7, 8]
console.log(copy === values); // false (different array objects)copy is a new array, so pushing to it leaves values unchanged. That makes spread a good replacement for concat and slice() when you want to avoid mutating the original.
Common Errors
- Spreading a non-iterable. Spread in arrays and calls requires an iterable.
[...{ a: 1 }]andMath.max(...undefined)both throw aTypeError. Use a fallback when the value might be missing:[...(list ?? [])]. - Forgetting the dots.
[0, values]produces a nested array[0, [3, 9, 4]]instead of a flat one. - Huge arrays in calls. Spreading hundreds of thousands of elements into a function call can exceed the engine's argument limit and throw a
RangeError. For very large data, usereduceor a loop.
Exercise
Given const head = ['a'] and const tail = ['c', 'd'], write one expression that builds ['a', 'b', 'c', 'd'] without changing either array. Then explain what console.log(Math.max([2, 7])) prints and why.
Show answer
[...head, 'b', ...tail] builds the new array. Math.max([2, 7]) prints NaN, because the array is passed as one argument and converts to the string "2,7", which is not a valid number. Writing Math.max(...[2, 7]) returns 7.
Exercise
What does const items = null; const all = [...items, 1]; do, and how would you make it safe?
Show answer
It throws a TypeError, because null is not iterable. Use [...(items ?? []), 1] to fall back to an empty array, which gives [1].