Two reasons:
1. Mutation callback
Calling fun
can change the array, since fun
completely user-defined. So you need to check again.
Example:
array.forEach(function (el, i) { delete array[i + 1]; });
2. Rare arrays
Another problem is that there may be sparse arrays: for example,
3 in ["a", "b", "c", , "e", "f"] === false // even though 3 in ["a", "b", "c", undefined, "e", "f"] === true
In these cases, you do not want to call fun
for this index / element, since there is nothing in this index.
["a", "b", "c", , "e", "f"].forEach(function (el, i) { console.log(el + " at " + i); }); // => "a at 0" "b at 1" "c at 2" "e at 4" "f at 5"
source share