Insights on various ways to Iterate an Array in Javascript

Saravanan Ramupillai
2 min readMar 13, 2021

An Array is essential data structure we extensively use in our day to day programming. In JS specifically we have lot of options when we need to work on array of data. Based on what we want as a result of iteration, we need to choose the right iteration option for better readability and efficiency.

Loopings:

  1. for operator :-
    with for-operator we can iterate over array with index. we need to calculate the index ourself to get element of an array.
  2. for…of operator :-
    This operator lets us to directly get an element so we do not need to calculate the index.
  3. for…in operator :-
    This operator lets us to directly get an index of an array, so we do not need to calculate the index.

Map:

Choose map when you want to do operation on each element of an array and to get result as an array of same size.

Filter:

Choose filter when to what to filter out element inside array based on condition.

Reduce:

Choose this when we want to convert all element inside into single value

So based on the need choose the right iterating option of array.

Here is the tip: Looping is much faster than array methods (map, reduce, filter, etc), so choose wisely

--

--