Loading video player…

Visit Every Item With for...of

In this video, you printed your day-hike packing list one item per line with a for...of loop.

Key Points

  • A for...of loop visits each item of an array, one pass per item, in the order the items sit in the list.
  • On each pass, the loop puts the current item into the name you write after const, here item, and then runs the body between the curly braces.
  • Each pass gets its own fresh item, so const works even though the value is different every time.
  • After the last item, the loop ends and the program carries on.
  • An item you .push() onto the list gets its own pass at the end, without any change to the loop.
  • An empty array has no first item, so the loop makes zero passes and its body never runs. That’s not an error.

Final Code

Language: JavaScript Filename: packing.js
const packingList = ["water bottle", "rain jacket", "map"];
packingList.push("snacks");
for (const item of packingList) {
  console.log(item);
}
Language: Shell
$ node packing.js
water bottle
rain jacket
map
snacks

And with an empty list:

Language: JavaScript Filename: bag.js
const emptyBag = [];
for (const item of emptyBag) {
  console.log(item);
}
console.log("Bag checked.");
Language: Program Output
Bag checked.

00:00 Earlier in this course, you packed a list for a day hike and printed it with one log line. Node showed the whole array at once, square brackets and all. Now picture a checklist instead, with one item on each line.

00:14 You could write a separate log line for every item. But then each new thing you pack would need another line of code, too.

00:23 JavaScript has a loop made for this, called a for...of loop. Here it is in your script, under the same list. You can read it almost like a sentence.

00:32 For each item of the packing list, log the item. Here’s how it runs. JavaScript takes the first item from the list and stores it under the name item, which the loop sets up for you.

00:44 Then it runs the body between the curly braces, which logs water bottle. That’s one pass. JavaScript moves on to the next item, rain jacket, and runs the body again.

00:57 Then it does the same with the map. After the last item, there’s nothing left to visit, so the loop ends. You might wonder how item can be a const when its value keeps changing.

01:09 Each pass gets its own fresh item. So nothing is ever reassigned.

01:17 Now pack one more thing. Push snacks onto the list before the loop, the way you did before. The loop itself stays exactly the same. Which item will the loop print last?

01:28 Your turn: pause the video and predict the last line.

01:37 It’s snacks. The push added snacks to the end of the list. The loop visits items in list order, so snacks gets the fourth and final pass. You didn’t touch the loop at all, and it picked up the new item on its own.

01:53 That’s what the checklist needed.

01:57 What about a bag with nothing in it yet? This new script loops over emptyBag, an empty list. After the loop, it logs Bag checked. so you know it finished.

02:08 What does this program print? Your turn: pause the video and predict the output.

02:19 Only Bag checked. The empty list has no first item. So the loop makes zero passes, and its body never runs. That’s not an error. The program carries on after the loop.

02:35 So here’s the rule. A for...of loop runs its body once for each item, in list order, and zero times for an empty list.

02:44 Next up is Print a Playlist, Line by Line. It’s an exercise where you’ll write a loop like this one for a playlist. Have fun with it.

Become a Member to join the conversation.