foreach loop array code example
Example 1: for each js
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
fruits.forEach(function(fruit){
console.log('I want to eat a ' + fruit)
});
Example 2: es6 forEach
const array1 = ['a', 'b', 'c'];
array1.forEach((element) => {
console.log(element)
});
Example 3: javascript foreach example
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});
Example 4: js for each item in array
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
Example 5: foreach in javascript
var a = ["a", "b", "c"];
a.forEach(function(entry) {
console.log(entry);
});
var fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
function myFunction(item, index) {
document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
}
Example 6: foreach loop javascript
const numList = [1, 2, 3];
numList.forEach((number) => {
console.log(number);
});