4.5 The for of Loop
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.
1.Introduction2 lessons, 09:45
2.ES6 Basics3 lessons, 23:18
3.Built-in Objects4 lessons, 22:32
4.Data Structures5 lessons, 21:02
5.New Function Types2 lessons, 12:05
6.More New Syntax3 lessons, 23:01
7.Conclusion1 lesson, 00:46
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.