Which objects can be traversed by for of
for...of...: It is a new traversal method in es6, but it is limited to iterators (iterator), so ordinary objects will report errors when using for...of to traverse.
Iterable objects: including Array, Map (mapping), Set (collection), String, TypedArray, arguments object, etc.
The syntax of the JS for of loop is as follows:
for (variable of iterable) {
// 要执行的代码
}
Among them, variable is a variable, and this variable will be assigned a different value each time it is looped. We can use this variable in the following { } to perform a series of operations; iterable is the content to be traversed. In each loop, Will assign a value in the iterable to the variable variable until all the values in the iterable have been traversed.
for example:
var a = ["a","b","c","d","e"]
for(let i of a){
document.write(i);
console.log(i)
}
Note that although the for of loop can also traverse objects, it is not recommended.
If traversing objects, you can use for in loop.