for-of
loop for-of
loop
for-of
only works with iterable values
const
declarations versus var
declarations
for-of
is a new loop in ES6 that replaces both for-in
and forEach()
and supports the new iteration protocol.
Use it to loop over iterable objects (Arrays, strings, Maps, Sets, etc.; see Chap. “Iterables and iterators”):
break
and continue
work inside for-of
loops:
Access both elements and their indices while looping over an Array (the square brackets before of
mean that we are using destructuring):
Looping over the [key, value] entries in a Map (the square brackets before of
mean that we are using destructuring):
for-of
loop for-of
lets you loop over data structures that are iterable: Arrays, strings, Maps, Sets and others. How exactly iterability works is explained in Chap. “Iterables and iterators”. But you don’t have to know the details if you use the for-of
loop:
for-of
goes through the items of iterable
and assigns them, one at a time, to the loop variable x
, before it executes the body. The scope of x
is the loop, it only exists inside it.
You can use break
and continue
:
for-of
combines the advantages of:
for
loops: break
/continue
; usable in generatorsforEach()
methods: concise syntaxfor-of
only works with iterable values The operand of the of
clause must be iterable. That means that you need a helper function if you want to iterate over plain objects (see “Plain objects are not iterable”). If a value is Array-like, you can convert it to an Array via Array.from()
:
const
declarations versus var
declarations If you const
-declare the iteration variable, a fresh binding (storage space) will be created for each iteration. That can be seen in the following code snippet where we save the current binding of elem
for later, via an arrow function. Afterwards, you can see that the arrow functions don’t share the same binding for elem
, they each have a different one.
A let
declaration works the same way as a const
declaration (but the bindings are mutable).
It is instructive to see how things are different if you var
-declare the iteration variable. Now all arrow functions refer to the same binding of elem
.
Having one binding per iteration is very helpful whenever you create functions via a loop (e.g. to add event listeners).
You also get per-iteration bindings in for
loops (via let
) and for-in
loops (via const
or let
). Details are explained in the chapter on variables.
So far, we have only seen for-of
with a declared iteration variable. But there are several other forms.
You can iterate with an existing variable:
You can also iterate with an object property:
And you can iterate with an Array element:
Combining for-of
with destructuring is especially useful for iterables over [key, value] pairs (encoded as Arrays). That’s what Maps are:
Array.prototype.entries()
also returns an iterable over [key, value] pairs:
Therefore, entries()
gives you a way to treat iterated items differently, depending on their position:
This function is used as follows: