javascript for of array code example

Example 1: for of array javascript

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

Example 2: for of js

let panier = ['fraise', 'banane', 'poire'];

for (const fruit of panier) {
  // console.log(fruit);
  console.log(panier.indexOf(fruit));
}

Example 3: for of loop javascript

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

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

// expected output: "a"
// expected output: "b"
// expected output: "c"

Example 4: javascript loop and array

var array = ["hello","world"];
array.forEach(item=>{
	console.log(item);
});

Example 5: javascript loop through object

var person = {"name":"bob", age:45};

for (var property in person) {
   console.log(person[property]);
}

Example 6: for item loop

const object = {a: 1, b: 2, c: 3};

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"