javascript foreeach code example

Example 1: javascript foreach

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

Example 2: javascript foreach

var colors = ['red', 'blue', 'green'];

colors.forEach(function(color) {
  console.log(color);
});

Example 3: javascript foreach index

users.forEach((user, index)=>{
	console.log(index); // Prints the index at which the loop is currently at
});

Example 4: how to use the foreach method in javascript

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