liveBook · Manning

Spread operator

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A JavaScript operator form that expands an iterable or object into individual elements or properties in an expression or argument list.

Suppose you have an array of marketplace prices and want the lowest one. `Math.min` expects separate arguments, not one array. Put `...` before the array to pass its values individually. This is spread: it ungroups an iterable where its items are needed. section 23.3

const prices = // get array of prices
const lowestPrice = Math.min(...prices);

Here, `Math.min(...prices)` receives each price as a separate argument. section 23.3

Spread works with iterables, not just arrays. Spreading a string supplies its characters one by one. Arrays, strings, Sets, and Maps share the iterable protocol; the same syntax can therefore use their produced values. section 23.3 section 23.1

Build an array from individual values

`[...'123', 'ABC']` produces `['1', '2', '3', 'ABC']`. Spread adds the string’s three characters separately; the unspread `'ABC'` remains one array item. Likewise, `[...cart, item]` makes an array containing the cart’s existing items followed by the new item. section 23.3

The same `...` syntax can run in the opposite direction. In a function definition, rest gathers separate arguments into an array: `function findDuplicates(...values)`. At a call site, spread ungroups an existing array: `findDuplicates(...letters)`. Passing `letters` without spread would supply one array argument instead of its individual letters. section 23.3

Spread also offers a concise way to make an array for code that needs to modify a copy: `const copy = [...items]`. Try predicting the lengths of `[...'123']`, `[...['123']]`, and `[...[1, 2, 3]]`. One limit matters: spreading an infinite iterable never finishes requesting values, so take a finite number of values from it instead. section 23.3 section 23.5

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Explains argument expansion, array construction, copying, and rest contrast through examples; leaves iterator mechanics to the broader iterable treatment.
Examples
Uses marketplace prices, duplicate-finding calls, string-to-array expansion, cart updates, and an infinite-iterable exercise.
What it assumes
Assumes familiarity with arrays, strings, and function calls; introduces iterables as a shared way to produce values.
What you can do after
Apply it to pass iterable items as arguments or assemble arrays, while avoiding spread on an infinite iterable.
How it connects
Connects spread to iterables, the iterable protocol, rest parameters, arrays, and strings.

Also explained in

Compare how 3 books explain spread operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.