-->

Why JavaScript says that a number is not a number?

2020-01-27 04:08发布

问题:

I have a piece of JavaScript code which is expected to set an integer value to a variable.

Something is broken, so when I try to do alert(A);, it returns NaN. isNaN(A); returns true. But if I alert(typeof(A));, it says number.

So how can a variable be a number and not a number at the same time? Maybe I misunderstood what NaN really is?


Edit: thanks to the answers, I see that I was wrong, because:

  • The type of NaN is Number,
  • NaN does mean "Not a number", which is not the same thing as "not of type Number",
  • 0/0 is a good example of NaN: it is still a number, but JavaScript (and nobody else) can say what is the real value of zero divided by zero. 1/0 on the other hand returns Infinity, which is not NaN.

回答1:

As I understand it, NaN is a sentinel instance of the Number class that represents, well, exactly what it stands for - numeric results that cannot be adequately represented. So 0/0 is not a number, in the sense that it's NaN, but it is a Number in terms of its type.

Perhaps it should have been called NaRN (Not a Representable Number).



回答2:

If you have a variable and assign it the result of 0/0, the variable is still of numeric type, but the value is undefined (not a number). There are other conditions under which this can occur, but this illustrates what you are seeing.



回答3:

You are confusing the type of your object with the value. NaN is a specific value that a an object of type number can be assigned with, for instance in the case of division of zero by zero or when trying to convert a number from a string that does not represent a number.



回答4:

You should check out the Wikipedia article. It has more details.



回答5:

Some definitions from W3Schools:

Infinity: A numeric value that represents positive/negative infinity

The POSITIVE_INFINITY property represents infinity, returned on overflow. NEGATIVE_INFINITY, represents negative infinity (returned on overflow).

The NaN property represents "Not-a-Number" value. This property indicates that a value is not a legal number.

The isFinite() function determines whether a number is a finite, legal number. This function returns false if the value is +infinity, -infinity, or NaN.

Some tests:

 var n1 = 1/0;
  var n2 = 0/0;
  var n3 = (Number.MAX_VALUE)*2; //overflow

  var b1 = Number.POSITIVE_INFINITY == n1;
  var b2 = Number.POSITIVE_INFINITY == n2;
  var b2n = Number.NEGATIVE_INFINITY == n2;
  var b3 = Number.POSITIVE_INFINITY == n3;

  var msg = "n1=" + n1 + ", n2=" + n2 + ", n3=" + n3;

  msg += "<br/> n1 Is POSITIVE_INFINITY=" + b1;
  msg += "<br/> n2 Is POSITIVE_INFINITY=" + b2;
  msg += "<br/> n2 Is POSITIVE_INFINITY=" + b2n;
  msg += "<br/> n3 Is POSITIVE_INFINITY=" + b3;

  msg += "<br/> n1 IsFinite=" + isFinite(n1);
  msg += "<br/> n2 IsFinite=" + isFinite(n2);
  msg += "<br/> n3 IsFinite=" + isFinite(n3);


  msg += "<br/> n1 + n1 =" + (n1 + n1) ;
  msg += "<br/> n1 - n1 =" + (n1 - n1) ;
  msg += "<br/> n2 + n1 =" + (n2 + n1) ;

  document.write(msg);

Shows

n1=Infinity, n2=NaN, n3=Infinity
n1 Is POSITIVE_INFINITY=true
n2 Is POSITIVE_INFINITY=false
n2 Is POSITIVE_INFINITY=false
n3 Is POSITIVE_INFINITY=true
n1 IsFinite=false
n2 IsFinite=false
n3 IsFinite=false
n1 + n1 =Infinity
n1 - n1 =NaN
n1 - n1 =NaN


回答6:

Along with what has been said I think if you divide by for example a string. It trys to returns NaN but still looks at it as a number.