JavaScript if(x) vs if(x==true)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23061921/
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
JavaScript if(x) vs if(x==true)
提问by TheZver
In JavaScript , in which cases the following statements won't be logically equal ?
在 JavaScript 中,在哪些情况下以下语句在逻辑上不相等?
if(x){}
and
和
if(x==true){}
Thanks
谢谢
回答by thefourtheye
They are not at all equal.
他们根本不是平等的。
if (x)
checks if x
is Truthy where as the later checks if the Boolean value of x
is true
.
检查是否x
为Truthy,后者检查布尔值是否x
为true
。
For example,
例如,
var x = {};
if (x) {
console.log("Truthy");
}
if (x == true) {
console.log("Equal to true");
}
Not only an object, any string (except an empty string), any number (except 0
(because 0
is Falsy) and 1
) will be considered as Truthy, but they will not be equal to true.
不仅是一个对象,任何字符串(空字符串除外)、任何数字(除了0
(因为0
是 Falsy)和1
)都将被视为Truthy,但它们不会等于true。
As per ECMA 5.1 Standards, in if (x)
, Truthiness of x
will be decided, as per the following table
根据ECMA 5.1 标准,在if (x)
,真实性x
将被确定,如下表
+-----------------------------------------------------------------------+
| Argument Type | Result |
|:--------------|------------------------------------------------------:|
| Undefined | false |
|---------------|-------------------------------------------------------|
| Null | false |
|---------------|-------------------------------------------------------|
| Boolean | The result equals the input argument (no conversion). |
|---------------|-------------------------------------------------------|
| Number | The result is false if the argument is +0, ?0, or NaN;|
| | otherwise the result is true. |
|---------------|-------------------------------------------------------|
| String | The result is false if the argument is the empty |
| | String (its length is zero); otherwise the result is |
| | true. |
|---------------|-------------------------------------------------------|
| Object | true |
+-----------------------------------------------------------------------+
Note:The last line object
, which includes both objects and Arrays.
注意:最后一行object
,包括对象和数组。
But in the later case, as per The Abstract Equality Comparison Algorithm,
但在后一种情况下,根据抽象平等比较算法,
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
value of x
will be converted to a number and that number will be checked against true
.
的值x
将被转换为一个数字,该数字将根据 进行检查true
。
Note:
笔记:
In JavaScript, true
is 1
and false
is 0
.
在 JavaScript 中,true
is1
和false
is 0
。
console.log(1 == true);
# true
console.log(0 == false);
# true
回答by Darius
Several cases evaluate to false in the first form, such as empty string, 0, undefined, null.
有几种情况在第一种形式中评估为 false,例如空字符串、0、未定义、空值。
If you want to be a bit more semantic about it, try the bang bang in front of the expression:
如果你想对它有更多的语义,试试在表达式前面的 bang bang:
if(!!x){...}
this will convert the expression result to a truthy representing the same semantically. This is closer to an analogue to the expression you describe (x == true)
这会将表达式结果转换为在语义上表示相同的真实值。这更接近于您描述的表达方式的类比(x == true)
Also be aware that ==
is value comparions with type coercion, eg "3" == 3
, whereas ===
asserts equal typing too.
还要注意,这==
是与类型强制的值比较,例如"3" == 3
,而===
断言也等于类型。
So they are not the same, but often logically represent the same test, thanks to the semantics of the language and the !! you can use
因此,由于语言的语义和 !! 您可以使用