Iterate over an array in JavaScript
In this article, 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.
There are many ways to iterate through an array in JavaScript:
Using console.log() Method
Example: In this example, we will access simple array elements using their index number.
javascript
let array = [ 'geeks' , '4' , 'geeks' ];
console.log(array[0]);
console.log(array[1]);
console.log(array[2]);
|
There are multiple ways one can iterate over an array in JavaScript. The most useful ones are mentioned below.
The for loop executes a set of instructions repeatedly until the given condition becomes false. It is similar to loops in other languages like C/C++, Java, etc.
Example: In this example we are using for loop for iteration of an array.
javascript
let array = [1, 2, 3, 4, 5, 6];
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
|
A While Loop in JavaScript is a control flow statement that allows the code to be executed repeatedly based on the given boolean condition.
Example: In this example we are using while loop for iteration of an array.
javascript
let index = 0;
let array = [1, 2, 3, 4, 5, 6];
while (index < array.length) {
console.log(array[index]);
index++;
}
|
The forEach method calls the provided function once for every array element in the order.
Example: In this example we are using forEach() method for iteration of an array.
javascript
let index = 0;
let array = [1, 2, 3, 4, 5, 6];
array.forEach(myFunction);
function myFunction(item, index) {
console.log(item);
}
|
The every() method checks if all elements in an array pass a test (provided as a function).
Example: In this example we are using every() method for iteration of an array.
javascript
let x = 0;
let 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
A map applies a function over every element and then returns the new array.
Example: In this example we are using map() method for iteration of an array.
javascript
let x = 0;
let array = [1, 2, 3, 4, 5, 6];
let square = x => Math.pow(x, 2);
square = array.map(square);
console.log(array);
console.log(square);
|
Output
[ 1, 2, 3, 4, 5, 6 ]
[ 1, 4, 9, 16, 25, 36 ]
It is used to filter values from an array and return the new filtered array.
Example: In this example we are using filter() method for iteration of an array.
Javascript
let array = [1, 2, 3, 4, 5, 6];
let even = x => x % 2 === 0;
let evens = array.filter(even);
console.log(array);
console.log(evens);
|
Output
[ 1, 2, 3, 4, 5, 6 ]
[ 2, 4, 6 ]
It is used to reduce the array into one single value using some functional logic.
Example: In this example we are using reduce() method for iteration of an array.
Javascript
let array = [1, 2, 3, 4, 5, 6];
const helperSum = (acc, curr) => acc + curr;
const sum = array.reduce(helperSum, 0);
console.log(array)
console.log(sum);
|
Output
[ 1, 2, 3, 4, 5, 6 ]
21
It is used to check whether some array values pass a test.
Example: In this example we are usingsome() method for iteration of an array.
Javascript
let 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
Last Updated :
15 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...