js 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: javascript foreach

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

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

Example 3: foreach javascript

let words = ['one', 'two', 'three', 'four'];
words.forEach((word) => {
  console.log(word);
});
// one
// two
// three
// four

Example 4: foreach loop javascript

const numList = [1, 2, 3];

numList.forEach((number) => {
  console.log(number);
});

Example 5: foreach loop javascript

listName.forEach((listItem) => {
  Logger.log(listItem);
}):

Example 6: mdn foreach

arr.forEach(callback(currentValue [, index [, array]])[, thisArg])