javascript 为什么!!1=="1" 等于真而!!2=="2" 等于假?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23693089/
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
Why does !!1=="1" equal true and !!2=="2" equal false?
提问by Michael Rader
As the title states, why does:
正如标题所述,为什么:
> !!1=="1"
equal
平等的
True
and
和
> !!2=="2"
equal:
平等的:
False
Likewise, why does > "1"==true
equal true
and > "2"==true
equal false
同样,为什么> "1"==true
等于true
和> "2"==true
等于false
I'm baffled. Are these just bugs in JS or what's going on here?
我很困惑。这些只是 JS 中的错误还是这里发生了什么?
回答by thefourtheye
As per the Operator precedencerules, logical !
has higher priority over ==
. So, in both the cases, !!
is evaluated first.
根据运算符优先级规则,逻辑比!
具有更高的优先级==
。因此,在这两种情况下,!!
都首先进行评估。
Note:Truthiness of various objects have been explained in this answerof mine.
注意:我的这个答案已经解释了各种对象的真实性。
First Case
第一种情况
!!1 == "1"
!1
will be evaluated to false
, since 1
is considered Truthy. Negating again we get true
. So the expression becomes
!1
将被评估为false
,因为1
被认为是真实的。再次否定我们得到true
。所以表达式变成
true == "1"
Now, the coercion rules kick in as you have used ==
operator, which evaluates as per the The Abstract Equality Comparison Algorithmdefined in ECMAScript 5.1 Specification,
现在,强制规则在您使用==
运算符时开始生效,该运算符根据ECMAScript 5.1 规范中定义的抽象平等比较算法进行评估,
6. If
Type(x)
isBoolean
, return the result of the comparisonToNumber(x) == y
.
6. 如果
Type(x)
是Boolean
,则返回比较的结果ToNumber(x) == y
。
So, true
will be converted to a number, which is 1 as per ToNumber
algorithm for Boolean values. Now the expression becomes
因此,true
将转换为一个数字,根据ToNumber
布尔值的算法,该数字为 1 。现在表达式变成
1 == "1"
Now,
现在,
4. If
Type(x)
isNumber
andType(y)
isString
, return the result of the comparisonx == ToNumber(y)
.
4.如果
Type(x)
是Number
和Type(y)
是String
,返回比较的结果x == ToNumber(y)
。
So, "1"
will be converted to a number and that will give 1, as per the ToNumber
algorithm. That is why it shows true
in the first case.
因此,"1"
将转换为一个数字,并根据ToNumber
算法给出 1 。这就是它true
在第一种情况下显示的原因。
Second Case
第二种情况
The same rules are applied here.
此处应用相同的规则。
!!2 == "2"
becomes
变成
true == "2"
then
然后
1 == "2"
which becomes
变成
1 == 2
which is not true
, that is why the second case prints false
.
true
事实并非如此,这就是为什么第二种情况会打印false
.
回答by user2864740
tldr; this is due to the [ToNumber]conversions in the ==
operator algorithm.
tldr; 这是由于运算符算法中的[ToNumber]转换==
。
The first step is to simplify the expression. Since !!x=="x"
is parsed like (!!x)=="x"
and !!a_truthy_expression -> true
, the actual relevant expression for the equality is
第一步是简化表达式。由于!!x=="x"
像(!!x)=="x"
and一样被解析!!a_truthy_expression -> true
,所以等式的实际相关表达式是
!!1=="2" -> true=="1" -> Boolean==String
!!2=="2" -> true=="2" -> Boolean==String
So then looking at the rules for 11.9.3 The Abstract Equality Comparison Algorithmand following along with the application yields
因此,然后查看11.9.3 抽象平等比较算法的规则并遵循应用程序的收益
Rule 6 - If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
规则 6 - 如果 Type(x) 是布尔值,则返回 ToNumber(x) == y 的比较结果。
which results in Number==String
or 1=="1" and 1=="2", respectively1. Then the rule
这导致Number==String
or 1=="1" 和 1=="2", 分别为1。那么规则
Rule 7 - If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
规则 7 - 如果 Type(x) 是 Number 并且 Type(y) 是 String,则返回比较结果 x == ToNumber(y)。
is applied which results in Number==Number
or 1==1 and 1==2, respectively1; the latter is clearly false.
应用导致Number==Number
或 1==1 和 1==2,分别为1;后者显然是错误的。
Rule 1 - If Type(x) is the same as Type(y), then [by c.iii.] If x is the same Number value as y, return true [else return false].
规则 1 - 如果 Type(x) 与 Type(y) 相同,则 [by c.iii.] 如果 x 与 y 的数值相同,则返回 true [否则返回 false]。
(The same algorithm explains the String==Boolean
case when the complementing rules are applied.)
(相同的算法解释了String==Boolean
应用补充规则的情况。)
1To see the [ToNumber] rule applied, consider:
1要查看应用的 [ToNumber] 规则,请考虑:
+false -> 0
+true -> 1
+"1" -> 1
+"2" -> 2
回答by Arnaldo Ignacio Gaspar Véjar
Its a precedence operator problem.
它是一个优先运算符问题。
The !
operator is an unary operator. That means the left side must be an expression or a boolean evaluable section. See Javascript MDN.
该!
运营商是一个一元运算符。这意味着左侧必须是表达式或布尔值可评估部分。请参阅JavaScript MDN。
!!1==1 is not necessary !!(1==1)
!!2==2 is not necessary !!(2==2)
I think that these expressions should be consistent if the equal operator has more precedence than ! operator. But if we consider the opposite, evaluating first negations we have:
我认为如果相等运算符的优先级高于 ! 操作员。但是如果我们考虑相反的情况,评估第一个否定我们有:
!!1 == 1
!1 -> false
!!1 -> true
!!1 == 1
And with the two
并与两人
!!2==2
!2 -> false
!!2 -> true
(!!2) == 2 -> false
That is because the ! operator has precedence over == operator
那是因为!运算符优先于 == 运算符
回答by Meredith
!!1
is equal to true, and "1" is equal to true ("0" is false, so is every other string). So !!1 == "1"
evaluates to true == true
, which of course returns true.
!!1
等于真,“1”等于真(“0”为假,其他字符串也是如此)。所以!!1 == "1"
评估为true == true
,这当然返回真。
!!2
is also equal to true. As I mentioned earlier, "2" is not "1", so it's false. Therefore, we have true == false
, which of course returns false.
!!2
也等于真。正如我之前提到的,“2”不是“1”,所以它是错误的。因此,我们有true == false
,当然返回 false。
If you want to see if 2 (a number) is equal to "2" (a string representation of a number), then all you have to do is 2 == "2"
, which evaluates to 2 == 2
, which is true. The difference is that we're not comparing a boolean against a boolean. We're comparing a number against a number.
如果您想查看 2(数字)是否等于“2”(数字的字符串表示),那么您所要做的就是2 == "2"
,其计算结果为2 == 2
,这是真的。不同之处在于我们不是将布尔值与布尔值进行比较。我们正在将一个数字与一个数字进行比较。
Basically, putting !!
in front of a number converts to a boolean, which forces JavaScript to cast your string to a boolean instead of a number.
基本上,将!!
数字放在前面会转换为布尔值,这会强制 JavaScript 将字符串转换为布尔值而不是数字。
回答by Miraage
Because "1" may be considered as "true" when you do equality check, not identity, but "2" - can't.
因为当你做平等检查时,“1”可能被认为是“真”,而不是身份,但“2” - 不能。