Loop (for each) over an array in JavaScript

23    Asked by john_5495 in Java , Asked on Apr 17, 2025

Want to perform actions on every item in a JavaScript array? Learn how the forEach() method and other looping techniques like for, for...of, and map() can help you iterate efficiently over array elements.

Answered by stickman hook3

Looping through an array in JavaScript is something we do all the time—whether it's displaying a list, processing data, or just logging elements. There are multiple ways to go about this, and the method you choose often depends on what you’re trying to achieve.

Here are some of the most commonly used methods:

forEach() method

 This is probably the most straightforward way to loop over an array.

let fruits = ['apple', 'banana', 'cherry'];
fruits.forEach(function(item) {
  console.log(item);
});

  •  It’s great for running a function on each element, but note that it doesn’t return a new array and doesn’t support break or continue.

for loop

 The classic way to loop with more control.

for (let i = 0; i < fruits>

  •  This gives you full control over the index and is useful when you need to access array positions or loop in reverse.

for...of loop

 A clean and modern approach for looping over iterable objects:

for (let fruit of fruits) {
  console.log(fruit);
}

  • map() method

 Ideal when you want to create a new array based on the original:

  let upperFruits = fruits.map(fruit => fruit.toUpperCase());

  • Each method has its own use case, but for simple tasks, forEach or for...of usually does the job quite nicely!



Your Answer