Insights on various ways to Iterate an Array in Javascript

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.

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

Software Engineer @KiSSFLOW

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store