JavaScript Array Iteration

To iterate over an array in JavaScript, you can use a for loop, a while loop, or one of the built-in array iterator methods: forEach(), map(), filter(), reduce(), some(), every(), find(), and findIndex().

For loop

The for loop is the most basic way to iterate over an array. It works by looping over the array elements from beginning to end, executing a code block for each element.

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

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

This code will print the following output to the console:

1 2 3 4 5

While loop

The while loop is another way to iterate over an array. It works by looping over the array elements while a condition is true.

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

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

This code will produce the same output as the for loop example above.

Built-in array iterator methods

JavaScript provides a number of built-in array iterator methods that can be used to iterate over arrays in different ways.

ForEach() – The forEach() method executes a callback function for each element in the array.

Map() – The map() method creates a new array populated with the results of calling a callback function on each element in the original array.

Filter() – The filter() method creates a new array populated with the elements in the original array that pass a test specified by a callback function.

Reduce() – The reduce() method reduces an array to a single value by applying a callback function to each element in the array.

Some() – The some() method returns true if any element in the array passes a test specified by a callback function.

Every() – The every() method returns true if all elements in the array pass a test specified by a callback function.

Find() – The find() method returns the first element in the array that passes a test specified by a callback function.

FindIndex() – The findIndex() method returns the index of the first element in the array that passes a test specified by a callback function.

Examples

Here are some examples of how to use the built-in array iterator methods:

// forEach()
const numbers = [1, 2, 3, 4, 5];

numbers.forEach((number) => {
console.log(number);
});
// map()
const doubledNumbers = numbers.map((number) => {
return number * 2;
});

console.log(doubledNumbers);
// filter()
const evenNumbers = numbers.filter((number) => {
return number % 2 === 0;
});

console.log(evenNumbers);
// reduce()
const sumOfNumbers = numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);

console.log(sumOfNumbers);

// some()
const isAnyNumberDivisibleByThree = numbers.some((number) => {
return number % 3 === 0;
});

console.log(isAnyNumberDivisibleByThree);

// every()
const isEveryNumberDivisibleByTwo = numbers.every((number) => {
return number % 2 === 0;
});

console.log(isEveryNumberDivisibleByTwo);

// find()
const firstEvenNumber = numbers.find((number) => {
return number % 2 === 0;
});

console.log(firstEvenNumber);

// findIndex()
const indexOfTheFirstEvenNumber = numbers.findIndex((number) => {
return number % 2 === 0;
});

console.log(indexOfTheFirstEvenNumber);

Conclusion

JavaScript array iteration is a powerful tool that can be used to perform a variety of tasks on arrays. By understanding the different ways to iterate over arrays, you can write more efficient and concise code.