for-of statement
The for-of construct creates
a loop to iterate over an array.
Syntax
for (let variableForElement of array) {
/*
The iterated array elements will fall
into the variableForElement one by one.
*/
};
Example
Let's iterate over the array elements and display them on the screen:
let arr = [1, 2, 3, 4, 5];
for (let elem of arr) {
console.log(elem);
}
Example
Let's make a loop iteration counter:
let arr = [1, 2, 3, 4, 5];
let i = 0;
for (let elem of arr) {
console.log(elem);
i++;
}
console.log(i);
See also
-
the lesson from JavaScript tutorial
detailing how to work with thefor-ofloop -
the
for-instatement
that creates a loop to iterate over an object -
the
breakstatement
that breaks a loop -
the
continuestatement
that moves a loop to next iteration