为什么 "true" == true 在 JavaScript 中显示 false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11363659/
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 "true" == true show false in JavaScript?
提问by Isaac
MDC describes the ==
operator as follows:
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.
如果两个操作数的类型不同,JavaScript 会转换操作数,然后应用严格的比较。如果操作数是数字或布尔值,则尽可能将操作数转换为数字;else 如果任一操作数是字符串,则另一个操作数在可能的情况下转换为字符串。
With this in mind, I would evaluate "true" == true
as follows:
考虑到这一点,我将评估"true" == true
如下:
- Are they of the same type? No
- Is either operand a number or boolean? Yes
- Can we convert both to a number? No(
isNaN(Number("true")) // true
) - Is either operand a string? Yes
- Can we convert the other operand to a string? Yes(
String(true) === "true" // true
)
- 它们是同一类型的吗?不
- 操作数是数字还是布尔值?是的
- 我们可以将两者都转换为数字吗?没有(
isNaN(Number("true")) // true
) - 任一操作数是字符串吗?是的
- 我们可以将另一个操作数转换为字符串吗?是(
String(true) === "true" // true
)
I've ended up with the strings "true"
and "true"
, which should evaluate to true
, but JavaScript shows false.
我最终得到了字符串"true"
and "true"
,它应该计算为true
,但 JavaScript 显示为 false。
What have I missed?
我错过了什么?
采纳答案by MaxArt
Because "true"
is converted to NaN
, while true
is converted to 1
. So they differ.
因为"true"
被转换为NaN
,而true
被转换为1
。所以他们不同。
Like you reported, both are converted to numbers, because at least true
can be (see Erik Reppen's comment), and then compared.
就像你报告的那样,两者都被转换为数字,因为至少true
可以(见 Erik Reppen 的评论),然后进行比较。
回答by nobitavn94
The ==
comparison operator is defined in ECMA 5as:
的==
比较运算符在ECMA 5中定义为:
- If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y). - If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y. - 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).
- 如果Type(x) 是 Number 并且Type(y) 是 String,则
返回比较结果 x == ToNumber(y)。 - 如果Type(x) 为 String 且Type(y) 为 Number,则
返回ToNumber(x) == y的比较结果。 - 如果Type(x) 是 Boolean,则返回ToNumber(x) == y的比较结果。
- 如果Type(y) 是布尔值,则返回比较结果 x == ToNumber(y)。
So, "true" == true is evaluated as:
因此,“true” == true 被评估为:
===> false
===> 假的
回答by Zohaib Ijaz
Acording to The Abstract Equality Comparison Algorithm
根据抽象相等比较算法
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
if one of the oprends is a boolean and other is not, boolean is converter to number 0 or 1. so true == "true"
is false.
如果 oprend 之一是布尔值而另一个不是,则布尔值是数字 0 或 1 的转换器。所以true == "true"
是假的。