EN
JavaScript - for...of statement
0
points
In this article, we would like to show you how to use for...of statement in JavaScript.
Description
The for...of
statement creates a loop iterating over iterable objects (such as strings, arrays, maps, sets, and user-defined iterables).
Syntax
for (variable of iterable) {
// statement
}
Practical examples
Example 1 - iterating over a string
// ONLINE-RUNNER:browser;
var iterable = 'ABC';
for (const value of iterable) {
console.log(value);
}
Output:
A
B
C
Example 2 - iterating over an array
// ONLINE-RUNNER:browser;
let array = [1, 2, 3, 'text'];
for (let entry of array) {
console.log(entry);
}
Output:
1
2
3
text
Example 3 - iterating over a map
// ONLINE-RUNNER:browser;
const iterable = new Map([
['A', 1],
['B', 2],
['C', 3],
]);
for (const value of iterable) {
console.log(value);
}
Output:
A,1
B,2
C,3