Arrays in Javascripts, are single variables used to store different kind of elements.
For example, a simple array accesses may something like this:
<script> array = [ 'geeks', '4', 'geeks' ]; // Accessing array elements one by one console.log(array[0]); console.log(array[1]); console.log(array[2]); </script> |
Output:
geeks 4 geeks
There are multiple ways one can iterate over an array in Javascript. The most useful ones are mentioned below.
Using for loop.
This is similar to for loops in other languages like C/C++, Java, etc
<script> array = [ 1, 2, 3, 4, 5, 6 ]; for (index = 0; index < array.length; index++) { console.log(array[index]); } </script> |
Output:
1 2 3 4 5 6
Using while loop.
This is again similar to other languages.
<script> index = 0; array = [ 1, 2, 3, 4, 5, 6 ]; while (index < array.length) { console.log(array[index]); index++; }</script> |
Output:
1 2 3 4 5 6
using forEach method.
The forEach method calls the provided function once for every array element in the order.
<script> index = 0; array = [ 1, 2, 3, 4, 5, 6 ]; array.forEach(myFunction); function myFunction(item, index) { console.log(item); }</script> |
Output:
1 2 3 4 5 6
Using every method.
The every() method checks if all elements in an array pass a test (provided as a function).
<script> 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'); } </script> |
Output:
At least one element is not less than 5.
Using map.
A map applies a function over every element and then returns the new array.
<script> 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);</script> |
Output:
1 2 3 4 5 6 1 4 9 16 25 36
Recommended Posts:
- What are the efficient ways to iterate over all DOM elements ?
- How to iterate over a JavaScript object ?
- How to determine which element the mouse pointer move over using JavaScript ?
- How to Change Background Color of a Div on Mouse Move Over using JavaScript ?
- What is the difference between of using Restangular over ngResource ?
- How to skip over an element in .map() ?
- Lodash _.over() Method
- Creating objects in JavaScript (4 Different Ways)
- Ways to implement Object.values() in JavaScript
- How to declare variables in different ways in JavaScript?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Pallavi Yadav


