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

发布于 2024-09-09 00:47:09 字数 610 浏览 8 评论 0原文

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

有些东西坏了,所以当我尝试执行 alert(A); 时,它返回 NaNisNaN(A); 返回 true。但如果我alert(typeof(A));,它会显示number

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


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

  • NaN的类型是Number
  • NaN 确实意味着“不是数字”,这与“不是 Number 类型”不同,
  • 0/0NaN 的一个很好的例子:它仍然是一个数字,但 JavaScript(没有其他人)可以说出零除以零的实际值是多少。另一方面,1/0 返回Infinity,它不是NaN

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仅一夜美梦 2024-09-16 00:47:09

据我了解,NaNNumber 类的一个哨兵实例,它准确地表示了它所代表的含义 - 无法充分表示的数字结果。因此,0/0 不是一个数字,因为它是 NaN,但它是一个 Number就其类型而言。

也许它应该被称为NaRN(不可表示的数字)。

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).

无边思念无边月 2024-09-16 00:47:09

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

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.

远山浅 2024-09-16 00:47:09

您将对象的类型混淆了。 NaN 是可以为 number 类型的对象分配的特定值,例如在零除以零的情况下或尝试将数字转换为不代表数字的字符串。

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.

想挽留 2024-09-16 00:47:09

W3Schools 中的一些定义:

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

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

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

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

一些测试:

 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);

显示

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

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
冷…雨湿花 2024-09-16 00:47:09

除了前面所说的之外,我认为如果除以例如字符串。它尝试返回 NaN 但仍将其视为数字。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文