Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Ways of iterating over a array in JavaScript

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will learn, we will learn how to iterate over an array using JavaScript, there are multiple ways to do so. Arrays in Javascript, are single variables used to store different kinds of elements.

Example: Simple array access may be something like this.

javascript




array = ['geeks', '4', 'geeks'];
 
// Accessing array elements one by one
console.log(array[0]);
console.log(array[1]);
console.log(array[2]);

Output: 

geeks
4
geeks

There are multiple ways one can iterate over an array in Javascript. The most useful ones are mentioned below.

Example using for loop: This is similar to for loops in other languages like C/C++, Java, etc.

javascript




array = [1, 2, 3, 4, 5, 6];
for (index = 0; index < array.length; index++) {
    console.log(array[index]);
}

Output:  

1
2
3
4
5
6

Example using while loop: This is again similar to other languages. 

javascript




index = 0;
array = [1, 2, 3, 4, 5, 6];
 
while (index < array.length) {
    console.log(array[index]);
    index++;
}

Output: 

1
2
3
4
5
6

Example using forEach() Method: The forEach method calls the provided function once for every array element in the order. 

javascript




index = 0;
array = [1, 2, 3, 4, 5, 6];
 
array.forEach(myFunction);
function myFunction(item, index) {
    console.log(item);
}

Output: 

1
2
3
4
5
6

Example using every() Method: The every() method checks if all elements in an array pass a test (provided as a function). 

javascript




index = 0;
array = [1, 2, 3, 4, 5, 6];
 
const under_five = x => x < 5;
 
if (array.every(under_five)) {
    console.log('All are less than 5');
}
else {
    console.log('At least one element is not less than 5');
}

Output: 

At least one element is not less than 5.

Example using map() Method: A map applies a function over every element and then returns the new array. 

javascript




index = 0;
array = [1, 2, 3, 4, 5, 6];
square = x => Math.pow(x, 2);
squares = array.map(square);
console.log(array);
console.log(squares);

Output:

1 2 3 4 5 6 
1 4 9 16 25 36

Example using Filter() Method: It is used to filter values from an array and return the new filtered array

Javascript




array = [1, 2, 3, 4, 5, 6];
 
even = x => x % 2 === 0;
evens = array.filter(even);
console.log(array);
console.log(evens);

Output:

[ 1, 2, 3, 4, 5, 6 ]
[ 2, 4, 6 ]

Example using reduce() Method: It is used to reduce the array into one single value using some functional logic

Javascript




array = [1, 2, 3, 4, 5, 6];
 
const helperSum = (acc, curr) => acc + curr
sum = array.reduce(helperSum, 0);
 
console.log(array)
console.log(sum);

Output:

[ 1, 2, 3, 4, 5, 6 ]
21

Example using some() Method: It is used to check whether some array values pass a test

Javascript




array = [1, 2, 3, 4, 5, 6];
 
const lessthanFourCheck = (element) => element < 4;
const lessthanFour = array.some(lessthanFourCheck)
 
console.log(array);
if (lessthanFour) {
    console.log("At least one element is less than 4")
} else {
    console.log("All elements are greater than 4 ")
}

Output: 

[ 1, 2, 3, 4, 5, 6 ]
At least one element is less than 4

My Personal Notes arrow_drop_up
Last Updated : 25 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials