javascript foreach loop array code example

Example 1: javascript foreach

const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})

Example 2: iterate array javascript

array = [ 1, 2, 3, 4, 5, 6 ]; 
for (index = 0; index < array.length; index++) { 
    console.log(array[index]); 
}

Example 3: javascript foreach example

var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
    console.log(color);
});

Example 4: js for each item in array

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

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

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

Example 5: javascript foreach call specific value in array

run.addEventListener("click", function () {
    people.forEach((element) => console.log(element.firstname));
  }); // select a specific string in array

Example 6: javascript forEach loop array

array.forEach(function calback(item, index, array)
{

 /* working codes here */ 
     
 });