foreach on array javascript code example

Example 1: javascript foreach

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

Example 2: foreach javascript

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

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

Example 3: foreach in javascript

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

Example 4: how to use the foreach method in javascript

groceries.forEach(groceryItem => console.log(groceryItem));

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