Home >Web Front-end >JS Tutorial >Simple usage example of for...of in ES6
This article brings you a simple usage example of for...of in ES6. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
for...of is a way to iterate iterable objects. Iterable objects include Array, Map, Set, String, TypedArray, arguments objects, etc.
Syntax
for(variable of iterable){ // statement }
for(let a of [1,2,3]){ console.log(a) } // 1 // 2 // 3
for(let s of 'hello'){ console.log(s) } // h // e // l // l // o
for(let s of new Set([1,2,3])){ console.log(s) } // 1 // 2 // 3
Map
for(let s of new Map([[1,1],[2,2]])){ console.log(s) } // (2) [1, 1] // (2) [2, 2]
(function() { for (let argument of arguments) { console.log(argument); } })(1, 2, 3);
for(let p of document.getElementsByTagName('p')){ console.log(p) } // <p>...<p> // <p>...<p> // <p>...<p> // <p>...<p> ...
for...of can only iterate iterable objects
The above is the detailed content of Simple usage example of for...of in ES6. For more information, please follow other related articles on the PHP Chinese website!