for of javascript code example

Example 1: javascript for loop

for (var i; i < 10; i++) {
  
}

Example 2: javascript for

let str = "";

for (let i = 0; i < 9; i++) {
  str = str + i;
}

console.log(str);
// expected output: "012345678"

Example 3: javascript loop through array

const myArray = ['foo', 'bar'];

myArray.forEach(x => console.log(x));

//or

for(let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}

Example 4: for of array javascript

let colors = ['red', 'green', 'blue'];
for (const color of colors){
    console.log(color);
}

Example 5: for of js

let list = [4, 5, 6];

for (let i in list) {
   console.log(i); // "0", "1", "2",
}

for (let i of list) {
   console.log(i); // "4", "5", "6"
}

Example 6: for of javascript

const array1 = ['a', 'b', 'c'];

for (const element of array1) {
  console.log(element);
}