foreach loop array in javascript code example
Example 1: javascript foreach
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
Example 2: javascript foreach
let numbers = ['one', 'two', 'three', 'four'];
numbers.forEach((num) => {
console.log(num);
});
Example 3: javascript foreach
const colors = ['blue', 'green', 'white'];
function iterate(item) {
console.log(item);
}
colors.forEach(iterate);
Example 4: what is foreach method in javascript
var arr = [1, 2, 3, 4, 5, 6];
arr.forEach(function (value, index, array){
console.log(value, index, array);
})