What is the result of ['1', '2', '3'].map(parseInt);
Result:
Why is ['1', NaN, NaN] not ['1', '2', '3']?
The reasons are as follows:
1. The map() method returns a new array, and the elements in the array are the values processed by the original array elements after calling the function.
The map() method processes the elements sequentially in the order of the original array elements.
map(parseInt) is actually:
map(function(item, index){ return parseInt(item, index); }) Copy the code and run in turn: parseInt('1', 0); parseInt('2', 1 ); parseInt('3', 2); Source: https://juejin.im/post/6844904017600970766
2. The second parameter of parseInt only supports integers between 2-36.
Look at the original MDN:
parseInt(string [, radix]) method's definition of radix parameters:
An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string. Be careful—this does not default to 10 !
So enumerate the following code execution results:
parseInt('22',37) //NaN
parseInt('22',36) //2666