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.
- 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.
- for…of operator :-
This operator lets us to directly get an element so we do not need to calculate the index.
- for…in operator :-
This operator lets us to directly get an index of an array, so we do not need to calculate the index.
Choose map when you want to do operation on each element of an array and to get result as an array of same size.
Choose filter when to what to filter out element inside array based on condition.
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.