CHAPTER 8 · 6 lessons · 78 exercises

How code is written today

Destructuring, spread and `?.`. The shorthand you meet first when you read code someone else wrote.

From here on the chapters are not required. As an introduction, what you have is enough. We add them anyway because without this chapter's ways of writing, other people's code reads as symbols. You get stuck not on not understanding the meaning, but on having no idea what the line is even doing.

The first thing to learn is for...of. It is a loop that takes an array one item at a time, written for (const fruit of fruits) { ... }. It does the same as the for you wrote in chapter 2, except that you do not have to keep the number yourself. The whole boilerplate of starting at i = 0, raising it to length and taking things out with fruits[i] disappears. So do the accidents caused by writing the number wrong.

Next is destructuring. It is a way of taking just the entries you need out of an object and turning them into variables all at once. Instead of lining up several const name = user.name; lines, you write const { name, age } = user; in one. The same works on arrays, taking things out by order, as in const [first, second] = ranking;.

This carries more meaning than "shorter and handy". Because you can use it straight in a function's arguments. Write destructuring in the argument of a function that catches an object and what it uses inside shows up in the function's heading. In real code this shape turns up very often.

Then spread, the three dots .... It spreads an array or object out on the spot. Write [...fruits, "peach"] and you get a new array with all of the original's contents lined up plus one more. The original does not change. That is the important part: rather than rewriting the original as push does, it becomes a way of writing that builds something new and returns it.

Write the same ... on a function's argument side and it means the opposite — catch any number. That way of writing is called rest parameters. The same symbol for spreading and gathering is confusing at first, but "written on the value side it spreads, written on the catching side it gathers" is enough to remember.

Last is optional chaining, ?.. You use it when following an entry that may not be there. user.address.city errors on the spot and stops the program if address is missing. Write user.address?.city and when it is missing it does not stop; undefined comes back.

This is not a tool for smothering errors. Write it only where you know it is possible for something to be missing. Put ?. everywhere and things go quietly on even when something that really should be there is missing, giving you a fault with no findable cause. It is a tool to be used selectively.

Every way of writing in this chapter is something you could already write with the chapters before it. Destructuring is assignments lined up; spread is the same as refilling with a loop. So think of it not as "a chapter where new things become possible" but as a chapter where you can write the same things shorter and harder to get wrong.

Getting to where you can read them is the first goal. Getting to where you can write them can come after.

Lessons in this chapter

Loop without counting

  1. 80Looping without numbersTake an array one item at a time in the form for (const fruit of fruits), with no number to keep.Go to the exercises

Pull out only what you need

  1. 81Taking things out of an objectTurn just the entries you need into variables all at once, in the form const { name, age } = user;.Go to the exercises
  2. 82Taking things out of an arrayTurn an array's contents into variables from the front in order, in the form const [first, second] = ranking;.Go to the exercises

Spread out, and gather up

  1. 83Spreading things outBuild a new array or object, leaving the original as it was, in the form [...fruits, "peach"].Go to the exercises
  2. 84Arguments that catch any numberCatch any number of arguments and handle them as an array, in the form function total(...numbers) {}.Go to the exercises

Follow a value that may be missing

  1. 85Following an entry that may not be thereUse ?. so you can follow past an entry that may not be there without stopping.Go to the exercises

Next up is “Hold data by key, or as a set”. Hold key-value pairs with Map, and build a collection with no duplicates using Set. The containers you reach for after arrays and objects.