Iterate over an array in JavaScript
This post will discuss how to iterate over an array in JavaScript.
1. Using for statement
A for-loop repeats until a specified condition evaluates to false. The JavaScript syntax remains similar to C and Java-style for-loop. You can effectively use it to process values in the array, as shown below:
|
1 2 3 4 5 |
const arr = [1, 2, 3, 4, 5]; for (var i = 0; i < arr.length; i++) { console.log(arr[i]); } |
You can also use a while statement over a for-loop to iterate over an array:
|
1 2 3 4 5 6 7 8 |
const arr = [1, 2, 3, 4, 5]; var i = 0; while (i < arr.length) { console.log(arr[i]); i++; } |
2. Using Array.prototype.forEach() function
The forEach() method executes a specified function for each array element. You can use the callback function to process values in the array.
|
1 2 3 4 5 |
const arr = [1, 2, 3, 4, 5]; arr.forEach(function (item) { console.log(item); }); |
In ES6, we have arrow functions that can replace anonymous functions.
|
1 2 |
const arr = [1, 2, 3, 4, 5]; arr.forEach(item => console.log(item)); |
The forEach method has a benefit over the traditional for-loop in that we don’t have to explicitly maintain the indexing information as the callback function is automatically invoked with the index of the current element in its second argument.
|
1 2 |
const arr = [1, 2, 3, 4, 5]; arr.forEach((item, index) => console.log(item, `found at index`, index)); |
3. Using for…of statement
The ES6 standard introduced the for…of construct for iterating over an iterable. The for…of statement creates a loop that iterates over the property values. It is different from a for…in loop, which iterates over property names instead of property values.
|
1 2 3 4 5 |
const arr = [1, 2, 3, 4, 5]; for (const i of arr) { console.log(i); } |
4. Using Array Iterator
The ES6 for…of statement internally uses iterators. To explicitly get an Array Iterator in JavaScript, you can call the values() method on the array.
|
1 2 3 4 5 6 7 8 9 10 11 |
const arr = [1, 2, 3, 4, 5]; var it = arr.values(); while (1) { let entry = it.next(); if (entry.done) { break; } console.log(entry.value); } |
5. Using jQuery
Similar to JavaScript forEach() method, jQuery offers the $.each() method, which can be used to loop through the set of the array elements.
|
1 2 3 4 5 6 7 8 |
const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); const $ = require("jquery")(window); const arr = [1, 2, 3, 4, 5]; $.each(arr, function() { console.log(this.valueOf()); }); |
6. Using Underscore/Lodash Library
Similarly, you can use the _.forEach method from Underscore or Lodash library, which offers similar behavior. Its alias _.each can also be used.
|
1 2 3 4 5 6 |
const _ = require('underscore'); // or lodash const arr = [1, 2, 3, 4, 5]; _.forEach(arr, function(item) { console.log(item); }); |
That’s all about iterating over an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)