JavaScript Array Iteration Methods

Array iteration methods allow you to loop through arrays and perform operations on each element. These methods are essential for functional programming in JavaScript and provide clean, readable ways to work with data.

Available Methods

Transformation Methods

  • map() - Creates a new array by transforming each element
  • filter() - Creates a new array with elements that pass a test

Execution Methods

  • forEach() - Executes a function for each array element

Reduction Methods

  • reduce() - Reduces array to a single value (left to right)
  • reduceRight() - Reduces array to a single value (right to left)

Testing Methods

  • every() - Tests if all elements pass a condition
  • some() - Tests if at least one element passes a condition

Method Categories

Non-Mutating Methods

These methods do not modify the original array:

  • map(), filter(), reduce(), reduceRight(), every(), some()

Side-Effect Methods

This method executes code but doesn't return a meaningful value:

  • forEach()

Common Patterns

Chaining Methods

Array methods can be chained together for powerful data transformations:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const result = numbers
  .filter(num => num % 2 === 0)  // Get even numbers: [2, 4, 6, 8, 10]
  .map(num => num * 2)           // Double them: [4, 8, 12, 16, 20]
  .reduce((sum, num) => sum + num, 0); // Sum them: 60

Callback Function Parameters

Most iteration methods accept a callback function with these parameters:

  • element - The current array element
  • index (optional) - The current element's index
  • array (optional) - The original array
array.method((element, index, array) => {
  // Your logic here
});

Performance Tips

  1. Use the right method for the job:
    • map() for transforming data
    • filter() for selecting data
    • find() for finding single items
    • forEach() only when you need side effects
  2. Avoid unnecessary iterations:
    • Use some() or every() instead of filter().length > 0
    • Use find() instead of filter()[0]
  3. Consider method chaining carefully:
    • Each method creates a new array (except forEach())
    • Long chains can impact performance with large datasets

When to Use Each Method

MethodUse When You Need To...
map()Transform each element into something else
filter()Select elements that meet certain criteria
forEach()Perform side effects (logging, DOM manipulation)
reduce()Calculate a single value from all elements
every()Check if all elements meet a condition
some()Check if any element meets a condition

Getting Started

Each method page includes:

  • Detailed syntax and parameters
  • Multiple practical examples
  • Common use cases and patterns
  • Performance considerations
  • Comparison with similar methods