DEV Community

Ashish Kankal
Ashish Kankal

Posted on

Top 6 Iteration Methods in Javascript

In JavaScript, there are a variety of iteration methods that can be used to loop through arrays, objects, and other iterable data structures. These methods allow you to perform tasks such as iterating over each element in a collection, filtering elements based on a condition, and reducing a collection to a single value. Lets look at some of the frequently used methods:

for...of loop

The for...of loop is a versatile iteration method that can be used to iterate over any iterable object, such as an array, object, or generator function.

Here is an example of how to use the for...of loop to iterate over an array and print out each element:

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

for (const number of numbers) {
  console.log(number);
}
Enter fullscreen mode Exit fullscreen mode

forEach() method

The forEach() method is a method that can be used to iterate over an array and execute a function for each element in the array.

Here is an example of how to use the forEach() method to iterate over an array and print out each element:

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

numbers.forEach(number => {
  console.log(number);
});
Enter fullscreen mode Exit fullscreen mode

map() method

The map() method is a method that can be used to iterate over an array and create a new array based on the results of a function.

Here is an example of how to use the map() method to create a new array that contains the square of each number in the original array:

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

const squares = numbers.map(number => number * number);
console.log(squares);
Enter fullscreen mode Exit fullscreen mode

reduce() method

This method reduces an array to a single value by applying a provided function to each element in the array.

Here is an example of how to use the reduce() method to compute the sum of the array numbers:

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

const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 15
Enter fullscreen mode Exit fullscreen mode

filter() method

filter method can be used to filter out elements from an array based on a specific criteria. This can be very useful for a variety of tasks, such as data validation, filtering search results, and more.

Here is an basic example of filter, in which strings array is filtered for the elements which has length greater than or equals to 5:

const strings = ["hello", "world", "goodbye"];

const longStrings = strings.filter(string => string.length >= 5);
console.log(longStrings); // ["hello", "world"]
Enter fullscreen mode Exit fullscreen mode

some() method

some takes an array and a function as arguments. The function is called on each element of the array;

  • If the function returns true, the method returns true.
  • If the function returns false for all elements of the array, the method returns false

Here is the example explaining the usage of some(...):

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

const hasEvenNumber = numbers.some(number => number % 2 === 0);
console.log(hasEvenNumber); // true
Enter fullscreen mode Exit fullscreen mode

every() method

every takes an array and a function as arguments. The function is called on each element of the array, and

  • If the function returns false for any element, the method returns false.
  • If the function returns true for all elements of the array, the method returns true

Here is the example explaining the usage of every(...):

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

const allEvenNumbers = numbers.every(number => number % 2 === 0);
console.log(allEvenNumbers); // false
Enter fullscreen mode Exit fullscreen mode

Top comments (0)