JavaScript Array Iteration

Array iteration methods provide elegant, functional ways to process every element in an array. These methods replace traditional for loops with more expressive, declarative code that clearly communicates intent.

Iteration Methods

The forEach() method calls a provided function once for each array element in order. The function receives three parameters: the element value, the index, and the array itself. forEach() is used for side effects like logging, updating DOM elements, or modifying external variables. It doesn't return anything (returns undefined), and you cannot break or return early from it. For simple iteration in modern JavaScript, for...of loops are often preferred.

The map() method creates a new array by calling a function on every element and using the return values. The callback function receives the element, index, and array. map() is one of the most commonly used array methods because it's perfect for transforming data—converting objects, formatting values, extracting properties, or applying calculations. The original array remains unchanged, making map() safe and predictable.

The filter() method creates a new array containing only elements for which the callback function returns true. The callback receives the element, index, and array. filter() is ideal for searching, removing unwanted items, or extracting subsets based on conditions. It's more declarative than manually building arrays with loops and if statements—the intent is immediately clear.

The reduce() method reduces an array to a single value by applying a callback function that accumulates a result. The callback receives an accumulator, current element, index, and array. You provide an initial value for the accumulator. reduce() is incredibly versatile—it can sum numbers, concatenate strings, flatten arrays, count occurrences, group data, or transform arrays into objects. It's powerful but can be harder to read than simpler methods.

The every() method tests whether all elements pass a test function, returning true only if every single element passes. It short-circuits on the first failing element. The callback receives element, index, and array. every() is perfect for validation—checking if all values meet criteria, all objects have required properties, or all elements are within valid ranges. It's the logical AND of array testing.

The some() method tests whether at least one element passes a test function, returning true if any element passes. Like every(), it short-circuits as soon as it finds a passing element. some() is the logical OR of array testing—useful for checking if an array contains any element matching criteria, if any value is out of range, or if any object has a specific property.

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

// forEach
numbers.forEach((num, index) => {
  console.log(`Index ${index}: ${num}`);
});

// map
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

// filter
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4]

// reduce
const sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum); // 15

// every
const allPositive = numbers.every(num => num > 0);
console.log(allPositive); // true

// some
const hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven); // true