为什么 JavaScript 说数字不是数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3215120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:42:33  来源:igfitidea点击:

Why JavaScript says that a number is not a number?

javascriptnantypeof

提问by Arseni Mourzenko

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

我有一段 JavaScript 代码,预计将一个整数值设置为一个变量。

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.

有些东西坏了,所以当我尝试做时alert(A);,它会返回NaNisNaN(A);返回真。但如果我alert(typeof(A));,它说number

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

那么一个变量怎么能同时是一个数字而不是一个数字呢?也许我误解了 NaN 到底是什么?



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

编辑:感谢答案,我发现我错了,因为:

  • The type of NaNis Number,
  • NaNdoes mean "Not a number", which is not the same thing as "not of type Number",
  • 0/0is 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/0on the other hand returns Infinity, which is not NaN.
  • 的类型NaNNumber
  • NaN确实意味着“不是数字”,这与“非类型Number”不同,
  • 0/0是一个很好的例子NaN:它仍然是一个数字,但 JavaScript(和其他人)可以说出零除以零的实际值是多少。1/0另一方面返回Infinity,这不是NaN

采纳答案by Andrzej Doyle

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

据我了解,它NaNNumber该类的一个哨兵实例,它代表了它所代表的内容 - 无法充分表示的数字结果。So0/0不是数字,就其类型而言,NaN,但就Number其类型而言,它a 。

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

也许它应该被称为 NaRN(Not a Representable Number)。

回答by tvanfosson

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.

如果您有一个变量并为其分配结果 0/0,则该变量仍然是数字类型,但值未定义(不是数字)。还有其他条件可能会发生这种情况,但这说明了您所看到的情况。

回答by Janick Bernet

You are confusing the typeof your object with the value. NaNis a specific value that a an object of type numbercan 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.

您将对象的类型混淆了。NaNnumber可以分配给类型对象的特定值,例如在零除以零的情况下或尝试从不表示数字的字符串转换数字时。

回答by J?rgen Fogh

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

您应该查看维基百科文章。它有更多细节。

回答by user347594

Some definitions from W3Schools:

W3Schools 的一些定义:

Infinity: A numeric value that represents positive/negative infinity

Infinity:表示正/负无穷大的数值

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

POSITIVE_INFINITY 属性表示无穷大,在溢出时返回。NEGATIVE_INFINITY,表示负无穷大(溢出时返回)。

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

NaN 属性表示“非数字”值。此属性指示值不是合法数字。

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

isFinite() 函数确定一个数字是否是一个有限的合法数字。如果值为 +infinity、-infinity 或 NaN,则此函数返回 false。

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

回答by qw3n

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.

除了上面所说的,我认为如果你除以一个字符串。它尝试返回 NaN 但仍将其视为数字。