js for each loop code example

Example 1: for each js

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

// Iterate over fruits below

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

Example 2: foreach javascript

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

Example 3: javascript foreach example

var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
    console.log(color);
});

Example 4: javascript foreach index

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

Example 5: js for each item in array

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"

Example 6: foreach jas

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));