codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

27 / 65 topics
21JavaScript Strings22JavaScript String Methods23JavaScript Numbers24JavaScript Math Object25JavaScript Arrays26JavaScript Array Methods27JavaScript Array Iteration
Tutorials/JavaScript/JavaScript Array Iteration
🌐JavaScript

JavaScript Array Iteration

Updated 2026-05-12
30 min read

JavaScript Array Iteration

In this tutorial, we will explore various methods to iterate over arrays in JavaScript. Understanding these iteration methods is crucial for manipulating and processing data efficiently. Whether you're filtering a list of items, transforming data, or checking conditions within an array, these methods provide powerful tools to achieve your goals.

Introduction

Array iteration methods are essential for handling collections of data in JavaScript. They allow us to perform actions on each element of the array without manually writing loops. This not only makes our code cleaner but also more maintainable and readable.

In this tutorial, we will cover six common array iteration methods: forEach, map, filter, reduce, find, and some/every. Each method serves a unique purpose and can be used in different scenarios depending on your needs.

forEach

The forEach method executes a provided function once for each array element. It's often used for executing side effects, such as logging or updating other data structures.

Example 1: Logging Array Elements

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

Example 2: Updating an External Variable

JavaScript
1let sum = 0;
2const numbers = [1, 2, 3, 4, 5];
3
4numbers.forEach(function(number) {
5 sum += number;
6});
7
8console.log(sum);
Output

Example 2: Transforming Objects

JavaScript
1const users = [
2 { name: 'Alice', age: 25 },
3 { name: 'Bob', age: 30 },
4 { name: 'Charlie', age: 35 }
5];
6
7const names = users.map(function(user) {
8 return user.name;
9});
10
11console.log(names);
Output

Example 2: Filtering Active Users

JavaScript
1const users = [
2 { name: 'Alice', active: true },
3 { name: 'Bob', active: false },
4 { name: 'Charlie', active: true }
5];
6
7const activeUsers = users.filter(function(user) {
8 return user.active;
9});
10
11console.log(activeUsers);
Output

Example 2: Flattening an Array of Arrays

JavaScript
1const arrays = [[1, 2], [3, 4], [5, 6]];
2const flattenedArray = arrays.reduce(function(accumulator, currentValue) {
3 return accumulator.concat(currentValue);
4}, []);
5
6console.log(flattenedArray);
Output

Example 2: Finding a User by Name

JavaScript
1const users = [
2 { name: 'Alice', age: 25 },
3 { name: 'Bob', age: 30 },
4 { name: 'Charlie', age: 35 }
5];
6
7const user = users.find(function(user) {
8 return user.name === 'Bob';
9});
10
11console.log(user);
Output

Example 2: Checking if All Numbers are Positive

JavaScript
1const numbers = [1, -3, 5, 7];
2const allPositiveNumbers = numbers.every(function(number) {
3 return number > 0;
4});
5
6console.log(allPositiveNumbers);
Output

Summary

MethodDescription
forEachExecutes a function on each element of the array.
mapCreates a new array with transformed elements.
filterCreates a new array with elements that pass a test.
reduceAggregates array elements into a single value or structure.
findReturns the first element that passes a test.
some/everyCheck if at least one or all elements pass a test, respectively.

What's Next?

Now that you have a solid understanding of array iteration methods, it's time to explore JavaScript objects. Objects are another fundamental data structure in JavaScript, and learning how to work with them will enhance your ability to model real-world entities and structures in your code.

In the next tutorial, we'll dive into creating, accessing, modifying, and manipulating objects in JavaScript. Stay tuned!


PreviousJavaScript Array MethodsNext JavaScript Objects

Recommended Gear

JavaScript Array MethodsJavaScript Objects