Search code examples
javascriptnan

Javascript parseInt on large negative number gives NaN


When I do this:

var x = parseInt("–2147483648");
console.log(x);

I get the value as:

NaN

Why does this happen?

I want to test if a number is in the range of C (int), so I am doing the above, but it does not work. Also, I want to do this for C (long), is there a way to this?

For example: If I do:

var x = parseInt("-9223372036854775808");
console.log(x);

Now, I know that (-+)2^53 is the limit of numbers in Javascript. Is there some other way to test if the given value in a form is actually in the range of long or int?


Solution

  • It should work fine, the problem is that you're using the wrong character, an ndash vs a hyphen -:

    var x = parseInt("-2147483648");
    console.log(x);
    

    If you copy/paste that you'll see that it works now.