How to Use forEach() to Iterate an Array in JavaScript?

JavaScript has a powerful, yet easy-to-use array iteration syntax. With JavaScript arrays, you can iterate through a list of values every time you need to visit each item in the list. In this post, we'll show you how to use forEach() to iterate an array in JavaScript.

javascript foreach

By connect.sonveerconnect.sonveer on Apr 27, 2020
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})

Add Comment

75

javascript foreach

By GrepperGrepper on Jun 27, 2019
var colors = ['red', 'blue', 'green'];

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

Add Comment

52

for each js

By Unusual UnicornUnusual Unicorn on Mar 29, 2020
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below

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

Source: www.codecademy.com

Add Comment

30

foreach javascript

By VishalVishal on May 27, 2020
let words = ['one', 'two', 'three', 'four'];
words.forEach((word) => {
  console.log(word);
});
// one
// two
// three
// four

Add Comment

32

js foreach

By Av3Av3 on Oct 01, 2020
var stringArray = ["first", "second"];

myArray.forEach((string, index) => {
  	var msg = "The string: " + string + " is in index of " + index; 
	console.log(msg);
	
	// Output:
	// The string: first is in index of 0
	// The string: second is in index of 1
});

Add Comment

3

javascript foreach example

By GrepperGrepper on Jul 19, 2019
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
    console.log(color);
});

Add Comment

17

Use forEach() to iterate over the keys (as strings) or values of a JavaScript array.

View All Javascript queries

Javascript queries related to "for each js"

Browse Other Code Languages

CodeProZone