Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Visit Each Message With for...of

Your report needs to ask every message in a list the same question. A for...of loop hands each message to that question, one at a time, without copying the question for every record.

Key Points

  • for (const message of messages) { ... } runs the block in curly braces once for each item in messages, in the list’s order.
  • The name before of holds the current item on each pass. Here, message is a whole record. It isn’t a number that counts the passes or marks a position.
  • const fits because each pass gets a fresh message that never changes during that pass.
  • When the list runs out, the loop stops, and the code after its closing brace runs once.
  • An if inside the loop asks your question on every pass, so only matching messages are selected. This version checks only the topic, so message 9’s pending label doesn’t keep it out.
  • An empty list means zero passes. The block never runs, there’s no error, and there’s no extra code for the empty case.

The Code

The console.log() lines are supplied so you can see which lines ran:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 In the last lesson, you kept messages 7, 8 and 9 in one list, and its length said three.

00:10 Your report needs to ask each one the delivery question from earlier. That question looks at one record at a time, named message in your code.

00:20 So how do you hand every record to your question, in turn? Copying the question three times works today, but tomorrow’s list might hold forty messages, or none.

00:33 Here’s some new code in the Console, below your list.

00:40 The keyword for starts it. After the word of comes messages, the list to walk through. Before the word of, const declares a new name, message, as before.

00:55 It names the current record. The block in curly braces runs once per record. Its supplied console.log() line shows the current message. The last line runs after the loop.

01:10 Press Enter and watch. On the first pass, message names record 7, and the block logs its ID. On the second pass, message names record 8.

01:24 On the third pass, it names record 9.

01:28 There’s no record after 9, so the loop stops. Then the line after the closing brace runs, once. That’s a for...of loop. It visits every item in the list, once, in order.

01:45 Watch out: message holds the whole record, not a number that counts passes or marks a position. Each pass gets a fresh message that never changes during the pass, so const fits.

02:00 Now put your delivery question inside the loop, in place of the first console.log() line. Everything else stays the same.

02:12 On each pass, the if checks the current message’s topic. Only a delivery message gets logged as selected. Your turn: pause the video and predict which message IDs this loop logs.

02:31 Messages 7 and 9. Record 7 is about delivery, so the block runs. Record 8 is about billing, so the if skips its block. Record 9 is about delivery too, so it’s selected.

02:50 This version checks only the topic, so the pending label doesn’t keep 9 out. You wrote the question once, and the loop handed it every record.

03:02 Some days, nobody writes in, and the app hands you an empty list. Here’s your first loop again, over that empty list.

03:12 There’s no first record, so message never names anything, and the block runs zero times.

03:20 Only the line after the loop runs. There’s no error, and no extra code for the empty case. Here’s the rule to keep. A for...of loop runs its block once for each item, in order, and zero times for an empty list.

03:39 On every pass, the name before the word of holds the current item itself. So far, your loop only logs what it finds. Next up is Count Matching Messages, where you’ll count the messages your question selects.

Become a Member to join the conversation.