The "for…of" loop

Until now, we were iterating over an iterable object using the next() method, which is a cumbersome task. ES6 introduced the for…of loop to make this task easier.

The for…of loop was introduced to iterate over the values of an iterable object. Here is an example to demonstrate this:

function* generator_function()
{
  yield 1;
  yield 2;
  yield 3;
  yield 4;
  yield 5;
}

let arr = [1, 2, 3];

for(let value of generator_function())
{
  console.log(value);
}

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

The output is as follows:

1
2
3
4
5
1
2
3

Get React: Building Modern Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.