javascript foreach
snippet in javascript

javascript foreach

user5962

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

for each js

user4316

const fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below

// Normal way
fruits.forEach(function(fruit){
  console.log('I want to eat a ' + fruit)
});

javascript foreach

user9249

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

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

javascript foreach

user4154

let numbers = ['one', 'two', 'three', 'four'];

numbers.forEach((num) => {
  console.log(num);
});  // one   //two //three // four

javascript foreach

user1483

const arr = [10,20,30,40]

arr.forEach(function(item, index){
    console.log(item, index);
})

javascript foreach

user1638

const colors = ['blue', 'green', 'white'];

function iterate(item) {
  console.log(item);
}

colors.forEach(iterate);
// logs "blue"
// logs "green"
// logs "white"