Array and ArrayList in javascript

Ways of iterating over a array in JavaScript.

Arrays in Javascripts, are single variables used to store different kind of elements.
For example, a simple array accesses may something like this:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




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






Output:

1 2 3 4 5 6

Using while loop.
This is again similar to other languages.




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.




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).




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.




Output:

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

Using Filter

It is used to filter values from an array and return the new filtered array




Output:

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

Using Reduce

It is used to reduce the array into one single value using some functional logic




Output:

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

Using Some

It is used to check whether some array values passes a test




Output :

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


Article Tags :
JavaScript
javascript-array
JavaScript-Misc