为什么 "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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:37:42  来源:igfitidea点击:

Why does "true" == true show false in JavaScript?

javascriptstringequals

提问by Isaac

MDC describes the ==operator as follows:

MDC 对==操作符的描述如下

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" == trueas follows:

考虑到这一点,我将评估"true" == true如下:

  1. Are they of the same type? No
  2. Is either operand a number or boolean? Yes
  3. Can we convert both to a number? No(isNaN(Number("true")) // true)
  4. Is either operand a string? Yes
  5. Can we convert the other operand to a string? Yes(String(true) === "true" // true)
  1. 它们是同一类型的吗?
  2. 操作数是数字还是布尔值?是的
  3. 我们可以将两者都转换为数字吗?没有( isNaN(Number("true")) // true)
  4. 任一操作数是字符串吗?是的
  5. 我们可以将另一个操作数转换为字符串吗?( 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 trueis converted to 1. So they differ.

因为"true"被转换为NaN,而true被转换为1。所以他们不同。

Like you reported, both are converted to numbers, because at least truecan be (see Erik Reppen's comment), and then compared.

就像你报告的那样,两者都被转换为数字,因为至少true可以(见 Erik Reppen 的评论),然后进行比较。

回答by nobitavn94

The ==comparison operator is defined in ECMA 5as:

==比较运算符在ECMA 5中定义为:

  1. If Type(x) is Number and Type(y) is String,
    return the result of the comparison x == ToNumber(y).
  2. If Type(x) is String and Type(y) is Number,
    return the result of the comparison ToNumber(x) == y.
  3. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  4. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  1. 如果Type(x) 是 Number 并且Type(y) 是 String,则
    返回比较结果 x == ToNumber(y)。
  2. 如果Type(x) 为 String 且Type(y) 为 Number,则
    返回ToNumber(x) == y的比较结果。
  3. 如果Type(x) 是 Boolean,则返回ToNumber(x) == y的比较结果。
  4. 如果Type(y) 是布尔值,则返回比较结果 x == ToNumber(y)。

So, "true" == true is evaluated as:

因此,“true” == true 被评估为:

  1. "true" == ToNumber(true)   (via rule 7)
  2. "true" == 1
  3. ToNumber("true") == 1   (via rule 5)
  4. NaN == 1
  1. "true" == ToNumber(true)   (通过规则 7)
  2. “真” == 1
  3. ToNumber("true") == 1   (通过规则 5)
  4. NaN == 1

===> 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"是假的。