for in array js code example

Example 1: js loop array

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

Example 2: javascript object array iteration

let obj = {
  key1: "value1",
  key2: "value2",
  key3: "value3"
}

Object.keys(obj).forEach(key => {
  let value = obj[key];
  //use key and value here
});

Example 3: javascript loop through array

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

array1.forEach(element => console.log(element));

Example 4: foreach javascript

var items = ["item1", "item2", "item3"]
var copie = [];

items.forEach(function(item){
  copie.push(item);
});

Example 5: javascript for of

const array = ['hello', 'world', 'of', 'Corona'];

for (const item of array) {
  console.log(item);
}

Example 6: for in javascript

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"