DEV Community

pouya alamdari
pouya alamdari

Posted on

for of

for (variable of iterable) {
  statement
}
Enter fullscreen mode Exit fullscreen mode

Comparison

const colors = ['red', 'green', 'blue'];
// for in loop 
for (let x in colors) { //x or color or index or anything else!
    console.log(x);
}
Enter fullscreen mode Exit fullscreen mode

Image description

const colors = ['red', 'green', 'blue'];
// for of loop
for (let x of colors){ 
    console.log(x);
}
Enter fullscreen mode Exit fullscreen mode

Image description

so we use for in in the object

use for of in the array

Image description

Top comments (0)