I have been reading underscore.js source code and noticed this compare in _.each():
(obj.length === +obj.length)
I know that + before a variable casts it to a number/integer. So in this cause
[1,2,3].length === +[1,2,3].length
is true. If I pass in an object:
var obj = {a: 1, b: 2, c: 3};
+obj.length
produces NaN
In the last case, I have
[1,2,3, {a: [4,5,6]}].length
which is 4. Casting it to a number.. Is still 4.
Deciding from the else case, I can see that this comparation is probably done to distinguish arrays from objects as in else case it uses:
for (var key in obj) { ...
I fail to see any reason to use such a comparation. Can anyone explain ?