liveBook · Manning

For...of

As explained in Secrets of the JavaScript Ninja, Third Edition

JavaScript loop syntax that retrieves successive values from an iterable.

Need each value in a collection, without managing an index? A for...of loop takes successive values from an iterable. It works with arrays, maps, sets, and other objects that provide an iterator. section 8.3.1

Loop over values

for (const number of [1, 2, 3]) { console.log(number); } The loop logs 1, 2, and 3. Unlike a traditional indexed for loop, it does not require a counter or an array lookup. section 8.3.1 section 10.1.4

Under the hood, an iterable has a Symbol.iterator property whose function returns an iterator. Repeated calls to the iterator’s next() method produce results with value and done properties. The loop uses those results to obtain values and know when to stop. This protocol is not exclusive to arrays: you can define your own iterable. section 8.3.1

Listing 8.9 Looping through an array using its iterator
const numbers = [1, 2, 3];
const iterator = numbers[Symbol.iterator](); #A

let iteratorResult = iterator.next(); #B
while (!iteratorResult.done) { #C
  console.log(iteratorResult.value); #D
  iteratorResult = iterator.next(); #E
}

Calling an array iterator directly makes the next(), value, and done steps behind the loop visible. section 8.3.1

Choose the iterable that yields what you need. Looping over an array yields its values; looping over array.entries() yields index–value pairs. A map yields [key, value] entries in insertion order, so destructuring can assign both parts in the loop header. A map’s keys() and values() methods instead supply iterators for just one part, without creating an array. section 10.1.4 section 10.2.2

Listing 10.12 Iterating over a map
const directory = new Map<string, string>();  #A
 
directory.set("Yoshi", "+81 26 6462 9152");  #B
directory.set("Kuma", "+81 52 2378 6462");  #B
directory.set("Hiro", "+81 76 2771 4624");  #B
 
for (const [key, value] of directory) {  #C
  console.assert(key !== null, "Key:" + key);  #C
  console.assert(value !== null, "Value:" + value);  #C
}  #C
 
for (const key of directory.keys()) {  #D
  console.assert(key !== null, "Key:" + key);  #D
  console.assert(  #D
    directory.get(key) != null,  #D
    "Value:" + directory.get(key),  #D
  );  #D
}  #D
 
for (const value of directory.values()) {  #E
  console.assert(value !== null, "Value:" + value);  #E
}  #E

Three loops over one map contrast destructured entries with its keys() and values() iterators. section 10.2.2

Keep these distinctions in mind:

  • for...of retrieves values; for...in retrieves keys. On an array, for...in also visits attached object properties. section 10.1.4
  • A for...of loop can exit early with break or wait with await inside it. Array.forEach does not offer the same early exit or sequential waiting for an async callback. section 10.1.4
  • The iterable protocol also supports destructuring and spread syntax; for...of is one way to consume it, not the protocol itself. section 8.3.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains the Symbol.iterator and next() protocol through direct array traversal, then contrasts practical loop choices.
Examples
Shows an array loop, manual array-iterator traversal, a custom iterable, and three ways to loop over a map.
What it assumes
Assumes familiarity with ordinary loops and arrays while introducing Symbol.iterator and iterator results.
What you can do after
Apply it to traverse iterable values, select map entries or keys or values, and avoid confusing for...of with for...in.
How it connects
Connects iterables and iterators to destructuring, spread syntax, map entries, and array.forEach.

Also explained in

Compare how 3 books explain for...of →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.