Loop through an array in JavaScript

40    Asked by JakeCARTER in Java , Asked on Apr 8, 2025

Want to process or display each item in a JavaScript array but not sure which looping method is best? Let’s explore how to iterate over arrays using different approaches like for, forEach, map, and more.

Answered by Jack GREEN

If you're working with arrays in JavaScript, chances are you'll want to loop through them to access or manipulate each item. Luckily, JavaScript gives you several ways to do this, and each has its own use case!

 1. for loop (classic approach)

let arr = [1, 2, 3];
for (let i = 0; i < arr>

  • Gives you access to the index.
  • Great when you need full control over the loop.

 2. forEach() method

arr.forEach(function(item) {
  console.log(item);
});

Cleaner syntax.

  • Best for running a function on every item.
  • Doesn't support break or continue.

 3. for...of loop

for (let item of arr) {
  console.log(item);
}

  • Easy to read and write.
  • Works well for simple iteration.

 4. map() method

  let newArr = arr.map(item => item * 2);

Used when you want to transform each item and return a new array.

 Pro Tips:

  • Use forEach for side effects (like logging).
  • Use map when you need a new array.
  • Use for or for...of if you need more control.

In short, JavaScript gives you a lot of flexibility. Choose the looping method based on what you're trying to achieve — whether it’s readability, performance, or transforming data!



Your Answer