为什么 JavaScript 说数字不是数字?
我有一段 JavaScript 代码,预计将整数值设置为变量。
有些东西坏了,所以当我尝试执行 alert(A);
时,它返回 NaN
。 isNaN(A);
返回 true。但如果我alert(typeof(A));
,它会显示number
。
那么变量怎么可能同时是数字又不是数字呢?也许我误解了 NaN 到底是什么?
编辑:感谢答案,我发现我错了,因为:
NaN
的类型是Number
,NaN 确实意味着“不是数字”,这与“不是
Number
类型”不同,0/0
是NaN 的一个很好的例子
:它仍然是一个数字,但 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
isNumber
, NaN
does mean "Not a number", which is not the same thing as "not of typeNumber
",0/0
is a good example ofNaN
: 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 returnsInfinity
, which is notNaN
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我了解,
NaN
是Number
类的一个哨兵实例,它准确地表示了它所代表的含义 - 无法充分表示的数字结果。因此,0/0
不是一个数字,因为它是NaN
,但它是一个Number
就其类型而言。也许它应该被称为NaRN(不可表示的数字)。
As I understand it,
NaN
is a sentinel instance of theNumber
class that represents, well, exactly what it stands for - numeric results that cannot be adequately represented. So0/0
is not a number, in the sense that it'sNaN
, but it is aNumber
in terms of its type.Perhaps it should have been called NaRN (Not a Representable Number).
如果您有一个变量并将结果分配给它 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.
您将对象的类型与值混淆了。
NaN
是可以为number
类型的对象分配的特定值,例如在零除以零的情况下或尝试将数字转换为不代表数字的字符串。You are confusing the type of your object with the value.
NaN
is a specific value that a an object of typenumber
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.W3Schools 中的一些定义:
Infinity:表示正/负无穷大的数值
POSITIVE_INFINITY 属性表示无穷大,在溢出时返回。
NEGATIVE_INFINITY,表示负无穷大(溢出时返回)。
NaN 属性表示“非数字”值。此属性表明某个值不是合法数字。
isFinite() 函数确定一个数字是否是有限的合法数字。
如果值为+无穷大、-无穷大或 NaN,则此函数返回 false。
一些测试:
显示
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:
Shows
除了前面所说的之外,我认为如果除以例如字符串。它尝试返回 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.