Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does parseInt yield NaN with Array#map?

Tags:

javascript

People also ask

Why is parseInt return NaN?

If the first character cannot be converted to a number, parseInt returns NaN unless the radix is bigger than 10. For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN .

Can parseInt return NaN?

Description. The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .

Why 1 7 11. map parseInt returns 1 NaN 3 in JavaScript?

['1', '7', '11']. map(parseInt) doesn't work as intended because map passes three arguments into parseInt() on each iteration. The second argument index is passed into parseInt as a radix parameter. So, each string in the array is parsed using a different radix.


The callback function in Array.map has three parameters:

From the same Mozilla page that you linked to:

callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed."

So if you call a function parseInt which actually expects two arguments, the second argument will be the index of the element.

In this case, you ended up calling parseInt with radix 0, 1 and 2 in turn. The first is the same as not supplying the parameter, so it defaulted based on the input (base 10, in this case). Base 1 is an impossible number base, and 3 is not a valid number in base 2:

parseInt('1', 0); // OK - gives 1
parseInt('2', 1); // FAIL - 1 isn't a legal radix
parseInt('3', 2); // FAIL - 3 isn't legal in base 2 

So in this case, you need the wrapper function:

['1','2','3'].map(function(num) { return parseInt(num, 10); });

or with ES2015+ syntax:

['1','2','3'].map(num => parseInt(num, 10));

(In both cases, it's best to explicitly supply a radix to parseInt as shown, because otherwise it guesses the radix based on the input. In some older browsers, a leading 0 caused it to guess octal, which tended to be problematic. It will still guess hex if the string starts with 0x.)


map is passing along a 2nd argument, which is (in many of the cases) messing up parseInt's radix parameter.

If you're using underscore you can do:

['10','1','100'].map(_.partial(parseInt, _, 10))

Or without underscore:

['10','1','100'].map(function(x) { return parseInt(x, 10); });


You could solve this problem using Number as iteratee function:

var a = ['0', '1', '2', '10', '15', '57'].map(Number);

console.log(a);

Without the new operator, Number can be used to perform type conversion. However, it differs from parseInt: it doesn't parse the string and returns NaN if the number cannot be converted. For instance:

console.log(parseInt("19asdf"));
console.log(Number("19asf"));

I'm going to wager that it's something funky going on with the parseInt's 2nd parameter, the radix. Why it is breaking with the use of Array.map and not when you call it directly, I do not know.

//  Works fine
parseInt( 4 );
parseInt( 9 );

//  Breaks!  Why?
[1,4,9].map( parseInt );

//  Fixes the problem
[1,4,9].map( function( num ){ return parseInt( num, 10 ) } );

You can use arrow function ES2015/ES6 and just pass number to the parseInt. Default value for radix will be 10

[10, 20, 30].map(x => parseInt(x))

Or you can explicitly specify radix for better readability of your code.

[10, 20, 30].map(x => parseInt(x, 10))

In example above radix explicitly set to 10