Array iteration in JavaScript means going through each element of an array one by one to read, process, or transform data. JavaScript provides multiple built-in methods to iterate over arrays in a clean and readable way. These methods are commonly used for displaying lists, processing data, and applying logic to collections.


What Is Array Iteration?

Array iteration is the process of visiting each element in an array to perform an operation such as:

  • Reading values

  • Updating values

  • Creating new arrays

  • Filtering data


Common Ways to Iterate Over Arrays

JavaScript provides loop-based and method-based approaches. Method-based iteration is preferred for cleaner and readable code.


forEach() — Perform an Action on Each Element

JavaScript
1let numbers = [1, 2, 3];
2
3numbers.forEach(function (num) {
4 console.log(num);
5});

Explanation:
Runs a function for every element in the array.
Does not return a new array.


map() — Transform Each Element

JavaScript
1let numbers = [1, 2, 3];
2let doubled = numbers.map(function (num) {
3 return num * 2;
4});
5console.log(doubled);

Explanation:
Creates a new array by applying a transformation to each element.


filter() — Select Elements by Condition

JavaScript
1let numbers = [10, 20, 30, 40];
2let result = numbers.filter(function (num) {
3 return num > 20;
4});
5console.log(result);

Explanation:
Creates a new array with elements that pass the condition.


reduce() — Combine Values into One

JavaScript
1let numbers = [1, 2, 3, 4];
2let sum = numbers.reduce(function (total, num) {
3 return total + num;
4}, 0);
5console.log(sum);

Explanation:
Reduces the array into a single value.


some() and every() — Check Conditions

JavaScript
1let numbers = [2, 4, 6];
2
3console.log(numbers.some(num => num > 5));
4console.log(numbers.every(num => num > 1));

Explanation:

  • some() returns true if any element matches the condition

  • every() returns true if all elements match the condition


When to Use Which Iteration Method

RequirementMethod
Perform action onlyforEach()
Create new transformed arraymap()
Select elementsfilter()
Combine valuesreduce()
Check any matchsome()
Check all matchevery()

Common Mistakes with Array Iteration

  • Using map() when no new array is needed

  • Forgetting that forEach() does not return a new array

  • Mutating original arrays unintentionally

  • Using complex logic inside callbacks


Summary of Array Iteration Methods

MethodReturnsUse Case
forEach()undefinedPerform actions
map()New arrayTransform values
filter()New arraySelect values
reduce()Single valueAggregate results
some()BooleanCheck any condition
every()BooleanCheck all conditions

Conclusion

Array iteration in JavaScript provides powerful and readable ways to process collections of data. By using methods like forEach(), map(), filter(), and reduce(), array processing becomes clean, expressive, and efficient in JavaScript applications.