The three methods you mention have different purposes.
The purpose of the Array.prototype.map
method is to create a new array with the results of calling the callback function on every array element.
The purpose of the Array.prototype.forEach
method is to iterate over an array, executing the provided callback function once per array element.
The purpose of the for...in
statement is to enumerate object properties.
I think that the for...in
statement should be avoided to traverse any array-like1 object, where the real purpose is iterate over numeric indexes and not enumerate the object properties (even knowing that those indexes are properties).
Reasons to avoid for...in
to iterate array-like objects:
- Iterates over inherited user-defined properties in addition to the array elements, if you use a library like MooTools for example, which extend the
Array.prototype
object, you will see all those extended properties.
- The order of iteration is arbitrary, the elements may not be visited in numeric order.
Give a look to this article:
1 By array-like objects I mean any object that contains sequential numeric properties, and a length
property