javascript array loop foreach 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: javascript foreach example

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

Example 3: foreach jas

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

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

Example 4: javascript foreach

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

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

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

Example 5: javascript forEach loop array

array.forEach(function calback(item, index, array)
{

 /* working codes here */ 
     
 });

Example 6: how to use the foreach fnction javascript loop through array

const names = ['Anthony','Stacey','Mason','Gracie','Koda','Nani'];

forEach(function(name) {
	console.log(name);
});

Tags:

Dart Example