Possible Duplicate:
JavaScript - Array.map and parseInt
I've run into something strange:
["1", "3", "5"].map(parseInt)
returns [1, NaN, NaN]
, but ["1", "3", "5"].map(function(str){ return parseInt(str);})
returns the expected [1, 3, 5]
.
Why?
I've only tested this in Google Chrome 7.
Edit: Thanks for all the answers. Now I know what happens. With the help of the explanations below I figured out this would work:
parseInt2 = function(str){ return parseInt(str) }
["1", "3", "5"].map(parseInt2);
This results in the expected [1, 3, 5] array. All these extra params (i.e. index in map) is not always good, it seems.