javascript NaN 是假的吗?为什么 NaN === false 返回 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22600248/
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
Is NaN falsy? Why NaN === false returns false
提问by sc1013
- Why
NaN === false
=> false, isn't NaN falsy? - Why
NaN === NaN
=> false, but!!NaN === !!NaN
=> true
- 为什么
NaN === false
=> 假,NaN 不是假的吗? - 为什么
NaN === NaN
=> 假,但!!NaN === !!NaN
=> 真
I've been racking my brain trying to figure this out.
我一直在绞尽脑汁想弄清楚这一点。
回答by alex
- Falsyand being strictly equal to
false
are very different things, that's why one has ay
instead of ane
. ;) NaN
is spec'd to never be equal to anything. The second part of your question is comparingfalse === false
, which is funnily enough,true
:)
- Falsy和严格等于是
false
非常不同的事情,这就是为什么一个人有一个y
而不是一个e
。;) NaN
规范永远不等于任何东西。你问题的第二部分是比较false === false
,这很有趣,true
:)
If you really want to know if something is NaN
, you can use Object.is()
. Running Object.is(NaN, NaN)
returns true
.
如果您真的想知道某些东西是否存在NaN
,您可以使用Object.is()
. 运行Object.is(NaN, NaN)
返回true
。
回答by RobG
1.Why NaN === false => false, isn't NaN falsy?
1.为什么NaN === false => false,NaN不假吗?
The term "falsy" isn't defined in ECMA-262, it's jargon for where type conversion coerces a value to false. e.g.
ECMA-262 中没有定义术语“falsy”,它是类型转换将值强制为 false 的行话。例如
var x = NaN;
if (!x) {
console.log('x is "falsy"');
}
The strict equality operator uses the Strict Equality Comparison Algorithmwhich checks that the arguments are of the same Type, and NaN
is Type number, while false
is Type boolean, so they evaluated as not equal based on Type, there is no comparison of value.
严格相等运算符使用严格相等比较算法,该算法检查参数是否为相同类型,并且NaN
是类型编号,而false
类型是布尔值,因此它们根据类型评估为不相等,没有值的比较。
2.Why NaN === NaN => false, but !!NaN === !!NaN => true
2.为什么NaN === NaN => false,但是!!NaN === !!NaN => true
Because the strict equality comparison algorithm states that NaN !== NaN
, hence the isNaNmethod.
因为严格相等比较算法声明NaN !== NaN
,因此isNaN方法。
Using ! coerces the argument to boolean using the abstract ToBooleanmethod, where !NaN
converts to trueand !!NaN
converts to false, so:
使用 !使用抽象ToBoolean方法将参数强制为 boolean ,其中!NaN
转换为true并!!NaN
转换为false,因此:
!!NaN === !!NaN --> false === false --> true
Note that the abstract equality operator ==
will coerce the arguments to be of the same Type according to the rules for the Abstract Equality Comparison Algorithm. In this case, NaNis Type number, so falseis converted to a number using toNumberwhich returns 0
. And 0
is not equal to NaNso:
请注意,抽象相等运算符==
将根据抽象相等比较算法的规则将参数强制为相同类型。在这种情况下,NaN是类型数字,因此使用返回 的toNumber将false转换为数字。并且不等于NaN所以:0
0
NaN == false --> NaN == 0 --> false
回答by Ja?ck
This condition:
这个条件:
NaN === false
Is always false
because numbers are not booleans. To test if a value is falsy you can use a ternary expression:
总是false
因为数字不是布尔值。要测试值是否为假,您可以使用三元表达式:
NaN ? "truthy" : "falsy" // falsy
Why NaN === NaN => false
为什么 NaN === NaN => false
This is explained in MDN; pragmatically speaking, though, two values of which you only know they're not numbers can't logically be the same thing.
这在MDN 中有解释;但是,从实用的角度来说,您只知道它们不是数字的两个值在逻辑上不可能是同一件事。
... but why is !!NaN === !!NaN => true
...但为什么 !!NaN === !!NaN => true
This is because casting NaN
into a boolean will make it false
and booleans can be compared as per normal.
这是因为转换NaN
为布尔值将使它false
和布尔值可以正常进行比较。
回答by nbrooks
===
compares both type and value.
===
比较类型和值。
Even though NaN
is falsey, ===
wouldn't be the way to compare it. Something is "falsey" if it evaluates to false in a boolean expression. That isn't the same as being equal to (or equivalent to) false.
即使NaN
是假的,===
也不是比较它的方法。如果在布尔表达式中计算结果为假,则某些内容是“假的”。这与等于(或等价于)false 不同。
For example, null == false
returns false, even though null
is falsey. This is not completely intuitive, but that's just how JavaScript handles false/falsey values.
例如,null == false
返回假,即使null
是假的。这并不完全直观,但这正是 JavaScript 处理 false/falsey 值的方式。
0
and the blank string ("") are special cases where value equality comparisons against false
evaluate to true
(i.e. 0 == false
and "" == false
). However, 0===false
and ""===false
still returns false.
0
和空白字符串 ("") 是特殊情况,其中值相等性比较false
评估为true
(即0 == false
和"" == false
)。然而,0===false
和""===false
仍返回false。
NaN
is special in that it doesn't have a real value, so comparing it to itself doesn't return true. Essentially, NaN
is equal to nothing, not even NaN
.
NaN
特殊之处在于它没有实际值,因此将其与自身进行比较不会返回 true。本质上,NaN
等于没有,甚至不等于NaN
。
The only way to reliably compare something to NaN is using isNaN( value )
.
将某些内容与 NaN 进行可靠比较的唯一方法是使用isNaN( value )
.
To your second point, !value
is a boolean expression. value
first undergoes type coercion to a boolean (and remember, NaN
is falsey) and then the boolean NOT !
makes it true. As it happens, it's double negated, so !!NaN
is the same as boolean false
. Of course false === false
, so the expression evaluates to true
.
对于你的第二点,!value
是一个布尔表达式。value
首先对布尔值进行类型强制(记住,NaN
是假的),然后布尔值 NOT!
使其为真。碰巧,它是双重否定的,因此!!NaN
与 boolean 相同false
。当然false === false
,所以表达式的计算结果为true
。
回答by Andre Figueiredo
- Why NaN === false => false, isn't NaN falsy?
- 为什么 NaN === false => false,NaN 不是假的吗?
NaN
as you are using, is a global propertyinitialized with value of Not-A-Number
. It's not boolean. It's NaN data type as defined by IEEE 754.
NaN
正如您所使用的,是一个全局属性,初始化为Not-A-Number
. 它不是布尔值。它是 IEEE 754 定义的 NaN 数据类型。
It's the "same thing" you compare null === false
(or even null == false
).
这是您比较null === false
(甚至是null == false
)的“相同事物” 。
In this case, there is no difference using sctric equal or not:NaN == false
, also will return false!
在这种情况下,使用 sctric equal 与 not: 没有区别NaN == false
,也会返回 false!
- Why NaN === NaN => false, but !!NaN === !!NaN => true
- 为什么 NaN === NaN => false,但是 !!NaN === !!NaN => true
2.1.NaN ==== NaN
, is false by definition.
2.1. NaN ==== NaN
, 根据定义是假的。
2.2.But in !!NaN === !!NaN
, you aren't comparing NaNs anymore, when you do ![value], you "evaluate" it (or cast to a boolean).
2.2. 但是!!NaN === !!NaN
,当您这样做时,您不再比较 NaN 了![value],您“评估”它(或转换为布尔值)。
I'm gonna now explain with null
, because it's more used, so you can apply it to NaN
:
我现在要用 来解释null
,因为它更常用,所以你可以将它应用到NaN
:
Casting NaN, it's the same thing that casting null
.
铸造 NaN,这与铸造null
.
null == false? true : false // false! because it's not a bool false.<br>
!null? true: false // true! because !null -> true -> if(true)...
More Refs:
更多参考:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.1.1
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.1.1
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
回答by bagz_man
NaN and false are both the subset of Falsy.
NaN 和 false 都是 Falsy 的子集。
- NaN == false -> they're both falsy -> thus 'TRUE'
- NaN === false -> they're NOTthe same type -> thus 'FALSE'
- NaN == false -> 它们都是假的 -> 因此是 'TRUE'
- 为NaN ===假- >他们是不是同一类型- >这样“假”