Spread and De-structure

You should be using the spread operator.

And moreover you should be excited about using it in your JavaScript! It abstracts away individual items in a collection and makes it easier to reason about passing multiple arguments to functions, or destructing arrays.

Quick overview

const names = ['oscar', 'elmo', 'big bird', 'burt', 'ernie']
const sayHi = (leadRole, ...otherCharacters) => {
console.log(`hi ${oneName}`)
for (let o of otherCharacters) {
console.log('and hello ', o);
sayHi('Big Bird', ...names) // 'hi Big Bird', 'hello oscar', 'hello elmo' etc

MDN has some examples, too that are not Sesame Street themed.


You can use the spread operator to destructure an array:

const [colors, ...hues] = ['orangered', 'cornflowerblue', 'bisque']
colors     // --> 'orangered'
hues // --> 'cornflowerblue', 'bisque'

The spread operator is a handy and elegant tool to use when processing collections. Using it to destructure an array alleviates the concern over how each item is processed, and allows for handling dynamic collections.

Show your support

Clapping shows how much you appreciated Jared Fowler’s story.