为什么 JavaScript 负数并不总是 true 或 false?
-1 == true; // false
-1 == false // false
-1 ? true : false; // true
谁能解释一下上面的输出吗?我知道我可以通过与 0 进行比较来解决这个问题,但我很感兴趣。我希望至少有一个草率的 equals 语句是正确的,因为它们进行隐式类型转换,而且我当然没想到三元会得出完全不同的结果。
-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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在前两种情况下,布尔值被转换为数字 - 1 表示 true 和 0 表示假。在最后一种情况下,它是一个被转换为布尔值的数字,除了 0 和 NaN 之外的任何数字都将转换为 true。因此,您的测试用例实际上更像是这样:
对于任何非 0 或 1 的数字也是如此。
有关更多详细信息,请阅读 ECMAScript 文档。来自 第 3 版 [PDF],11.9.3 抽象相等比较算法部分:
值得阅读完整的算法,因为其他类型可能会导致更严重的问题。
In the first two cases, the boolean is cast to a number - 1 for true and 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:
The same would be true of any number that isn't 0 or 1.
For more detail, read the ECMAScript documentation. From the 3rd edition [PDF], section 11.9.3 The Abstract Equality Comparison Algorithm:
It's worth giving the full algorithm a read because other types can cause worse gotchas.
在大多数系统中,非零值被视为真值,但这并不一定意味着它们与
true
相同真值。因此,-1 == true
不一定成立,但-1
仍然可以被视为真值,因为它是非零的。不过,实际上,如果可以避免的话,就不应该将整数与布尔值进行比较。
In most systems, non-zero values are considered a true value, but that doesn't necessarily mean that they are the same true value as
true
. Thus,-1 == true
doesn't necessarily hold, but-1
can still be considered a true value since it is non-zero.Really, though, you shouldn't be comparing integers to booleans if you can avoid it.
当作为测试条件求值时,像 -1、5 和 17,000,000 这样的整数都会返回布尔 true,因为它们逻辑上求值为 true,例如
(注意:0 逻辑上求值为 false)
使用“?”运算符执行此代码所做的操作。它将第一个参数作为 if 语句中的条件传递,将第二个参数作为 true 情况传递,并将第三个参数作为 false 情况传递> 案例。
于是就有了第三个结果。
但是,这些整数的类型与true不同。
True 的类型为Boolean,-1、5 和 17,000,000 的类型为Integer。
就类型比较而言,比较“==”是严格的。即使两个事物具有相同的“值”,但类型不同,“==”运算符也会返回 false:
即使是以下内容也会返回 false,因为“true”是 String 类型,true 是 Boolean 类型:
因此,是前两个结果。
注意:如果您想比较值(无论类型如何),请使用“===”运算符:
并且,
希望这有帮助!
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.
(Note: 0 logically evaluates to false)
Using the "?" operator does what this code just does. It passes the first argument as a condition in an if statement, passes the second argument as the true case, and passes the third argument as the false case.
Hence the third result.
However, these integers are not of the same type as true.
True is of type Boolean, -1, 5 and 17,000,000 are of type 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:
Even the following will return false, because "true" is of type String and true is of type Boolean:
Hence, the first two results.
Note: If you'd like to compare values irregardless of type, use the "===" operator:
and also,
Hope this helps!