Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Ecma 1
  • Overview
  • Transcript

4.5 The for of Loop

The for...of loop is a new construct in ES6 that allows us to iterate over the values in a collection instead of the keys. Let's take a look at how we can use it to iterate arrays, maps, or any other iterable objects.

4.5 The for of Loop

Hi folks. In this lesson, we're going to take a look at the new for of loop that we can use to iterate different collections such as arrays, maps and sets and other iterable objects. The for of loop is very similar to the for in loop, with the difference being that for of loops iterate values while for in loops iterate keys. Let's take a look. The syntax for, the for of loop is pretty much identical to the syntax used in a regular four in loop. We use the for keyword, followed by parentheses containing the variable to be used inside the loop to represent the value on each iteration. The of key word and the objects being iterated. Inside the curly braces are any number of statements to execute. We use let instead of var here to specify that the variables should be visible only to the block. We can also iterate seps and maps with for of. And we can also use for of with maps In the case of the map the value returned is an array containing the current key from the map as the first item and the value as the second item. So in this lesson, we looked at the new for of loop. We saw that it is mostly the same as a for in loop, except that it's to iterate iterable objects, like arrays, maps, and sets, rather than object literals. We saw that the value used inside the block is mostly the same, regardless of the objects being iterated. Except in the case of maps, where an array containing both the keys and values is returned instead. Thanks for watching.

Back to the top