for-of statement

The for-of construct creates a loop to iterate over an array.

Syntax

javascriptfor (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:

javascriptlet arr = [1, 2, 3, 4, 5];

for (let elem of arr) {
  console.log(elem);
}

Example

Let's make a loop iteration counter:

javascriptlet arr = [1, 2, 3, 4, 5];

let i = 0;
for (let elem of arr) {
  console.log(elem);
  i++;
}
console.log(i);

See also