for of loop in javascript code example
Example 1: javascript for loop
var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 2: javascript for loop
for (var i; i < 10; i++) {
}
Example 3: for loop javascript
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
Example 4: javascript loop through array
const myArray = ['foo', 'bar'];
myArray.forEach(x => console.log(x));
for(let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
Example 5: for of js
let list = [4, 5, 6];
for (let i in list) {
console.log(i);
}
for (let i of list) {
console.log(i);
}
Example 6: for of js
let panier = ['fraise', 'banane', 'poire'];
for (const fruit of panier) {
console.log(panier.indexOf(fruit));
}