为什么 JavaScript 负数并不总是对或错?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3619797/
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 are JavaScript negative numbers not always true or false?
提问by Ollie Edwards
-1 == true; // false
-1 == false // false
-1 ? true : false; // true
Can anyone explain the above output? I know I could work round this by comparing to 0 but I'm interested. I'd expect at least one of the sloppy equals statements to be true as they do implicit type conversion, and I certainly didn't expect the ternary to come up with a totally different result.
谁能解释一下上面的输出?我知道我可以通过与 0 进行比较来解决这个问题,但我很感兴趣。我希望至少有一个草率的 equals 语句在执行隐式类型转换时为真,而且我当然没想到三元会得出完全不同的结果。
回答by Andy E
In the first two cases, the boolean is cast to a number - 1 for trueand 0 for false. In the final case, it is a number that is cast to a boolean and any number except for 0 and NaN will cast to true. So your test cases are really more like this:
在前两种情况下,布尔值被强制转换为一个数字 - 1 表示true, 0 表示false。在最后一种情况下,它是一个转换为布尔值的数字,除 0 和 NaN 之外的任何数字都将转换为true。所以你的测试用例真的更像这样:
-1 == 1; // false
-1 == 0; // false
true ? true : false; // true
The same would be true of any number that isn't 0 or 1.
任何不是 0 或 1 的数字也是如此。
For more detail, read the ECMAScript documentation. From the 3rd edition [PDF], section 11.9.3 The Abstract Equality Comparison Algorithm:
有关更多详细信息,请阅读 ECMAScript 文档。从第3 版 [PDF],第11.9.3节抽象平等比较算法:
19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
19. 如果 Type(y) 是布尔值,则返回比较结果 x == ToNumber(y)。
It's worth giving the full algorithm a read because other types can cause worse gotchas.
值得阅读完整的算法,因为其他类型可能会导致更糟糕的问题。
回答by Amber
In most systems, non-zero values are considered a true value, but that doesn't necessarily mean that they are the sametrue value as true. Thus, -1 == truedoesn't necessarily hold, but -1can still be considered a true value since it is non-zero.
在大多数系统中,非零值被视为真值,但这并不一定意味着它们与 的真值相同true。因此,-1 == true不一定成立,但-1仍可被视为真值,因为它不为零。
Really, though, you shouldn't be comparing integers to booleans if you can avoid it.
但是,实际上,如果可以避免的话,您不应该将整数与布尔值进行比较。
回答by AStanton
When evaluated as a test condition, integers like -1, 5 and 17,000,000, all return Boolean true, because they logically evaluate to true, e.g.
当作为测试条件评估时,像 -1、5 和 17,000,000 这样的整数都返回布尔值 true,因为它们在逻辑上评估为 true,例如
if(-1) {
"This is true";
}
else {
"This is false";
}
=> "This is true";
(Note: 0 logically evaluates to false)
(注意:0 逻辑上评估为假)
Using the "?" operator does what this code just does. It passes the first argument as a condition in an ifstatement, passes the second argument as the truecase, and passes the third argument as the falsecase.
使用 ”?” 运算符执行此代码所做的操作。它将第一个参数作为if语句中的条件传递,将第二个参数作为真情况传递,并将第三个参数作为假情况传递。
Hence the third result.
因此第三个结果。
However, these integers are not of the same typeas true.
但是,这些整数与true 的类型不同。
Trueis of type Boolean, -1, 5 and 17,000,000are of type Integer.
True属于Boolean类型,-1、5 和 17,000,000属于Integer类型。
The comparison '==' is strict, in terms of type comparison. Even two things have the same "value", but not the same type, the "==" operator returns false:
就类型比较而言,比较 '==' 是严格的。即使两件事具有相同的“值”,但类型不同,“==”运算符也会返回false:
if(6 == true) {
"This is true";
}
else {
"This is false";
}
=> "This is false";
Even the following will return false, because "true"is of type Stringand trueis of type Boolean:
即使以下内容也将返回 false,因为"true"属于String类型,而true属于Boolean类型:
if("true" == true) {
"This is true";
}
else {
"This is false";
}
=> "This is false";
Hence, the first two results.
因此,前两个结果。
Note: If you'd like to compare values irregardless of type, use the "===" operator:
注意:如果您想比较任何类型的值,请使用“===”运算符:
if(6 === true) {
"This is true";
}
else {
"This is false";
}
=> "This is true";
and also,
并且,
if("true" === true) {
"This is true";
}
else {
"This is false";
}
=> "This is true";
Hope this helps!
希望这可以帮助!

