JavaScript Array Iteration: A Complete Guide

In JavaScript, arrays are fundamental data structures that store collections of elements. As a technical writer, I'll walk you through various methods of iterating over arrays in JavaScript, along with examples for each approach. Additionally, we'll explore common use cases for these iteration techniques to demonstrate their practical applications.

1. Using a for loop

The traditional for loop is a versatile way to iterate over an array. It allows you to access each element by its index and perform necessary operations.

const array = [1, 2, 3, 4, 5];

for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

Common Use Case: When you need to access and manipulate elements based on their index, the for loop is a go-to choice.

2. Using for...of loop

The for...of loop is a more concise and readable alternative to the traditional for loop. It directly iterates over the elements of an array without the need for an index variable.

const array = [1, 2, 3, 4, 5];

for (const element of array) {
  console.log(element);
}

Common Use Case: When you want to loop through all elements in the array and don't require the index, the for...of loop is a cleaner option.

3. Using forEach method

The forEach method is a functional approach to iterate over arrays. It executes a provided callback function once for each element in the array.

const array = [1, 2, 3, 4, 5];

array.forEach((element) => {
  console.log(element);
});

Common Use Case: When you want to perform an action on each element of the array, like displaying them on the user interface or processing data.

4. Using map method

The map method creates a new array by applying a callback function to each element of the original array. It is particularly useful when you need to transform array elements.

const array = [1, 2, 3, 4, 5];

const newArray = array.map((element) => {
  return element * 2;
});

console.log(newArray);

Common Use Case: When you need to modify the elements of the array and create a new array with the modified values.

5. Using filter method

The filter method creates a new array containing all elements that pass a given test. It's ideal for selecting specific elements from the original array based on certain conditions.

const array = [1, 2, 3, 4, 5];

const filteredArray = array.filter((element) => {
  return element % 2 === 0;
});

console.log(filteredArray);

Common Use Case: When you need to extract elements that meet specific criteria, such as getting all even numbers or all elements larger than a certain value.

6. Using reduce method

The reduce method accumulates the array element's values into a single value, following the logic provided in the callback function.

const array = [1, 2, 3, 4, 5];

const sum = array.reduce((accumulator, element) => {
  return accumulator + element;
}, 0);

console.log(sum);

Common Use Case: When you need to compute a single value based on the elements of the array, such as calculating the sum or finding the maximum value.

7. Using for...in loop

The for...in loop is primarily designed to iterate over object properties, but it can also be used for arrays. However, it is not recommended because it may produce unexpected results when used with arrays, especially if you've extended the array prototype.

const array = [1, 2, 3, 4, 5];

for (const index in array) {
  console.log(array[index]);
}

Common Use Case: It's not recommended to use for...in with arrays due to potential issues, but it can be useful when working with objects.

Did you find this article valuable?

Support Divij Sehgal by becoming a sponsor. Any amount is appreciated!