简体   繁体   English

为什么JS中的true =='true'语句返回false?

[英]Why does true == 'true' statement in JS return false?

Question is in the title. 问题在标题中。 I've just tried to run next statements in Chrome console and got strange (as for me) result: 我刚尝试在Chrome控制台中运行下一个语句并且对我来说很奇怪(对我来说)结果:

true == 'true' // returns false
'true' == true // returns false

Why does it go such way? 为什么会这样? Why doesn't typecast work there, but in the next statement works? 为什么不在那里进行类型转换,但在下一个声明中有效?

if ('true') true // returns true

Because they don't represent equally convertible types/values. 因为它们不代表同样可转换的类型/值。 The conversion used by == is much more complex than a simple toBoolean conversion used by if ('true') . ==使用的转换比if ('true')使用的简单toBoolean转换复杂得多。

So given this code true == 'true' , it finds this: 所以鉴于此代码为true == 'true' ,它会发现:

"If Type(x) is Boolean , return the result of the comparison ToNumber(x) == y ." “如果Type(x)Boolean ,则返回比较结果ToNumber(x) == y 。”

So you see it starts by becoming ToNumber(true) == 'true' , which is 1 == 'true' , and then tries again, where it now finds: 所以你看它开始成为ToNumber(true) == 'true' ,这是1 == 'true' ,然后再次尝试,它现在发现:

If Type(x) is Number and Type(y) is String , return the result of the comparison x == ToNumber(y) . 如果Type(x)NumberType(y)String ,则返回比较结果x == ToNumber(y)

So now it's doing 1 == ToNumber('true') , which is 1 == NaN , which of course is false . 所以现在它正在做1 == ToNumber('true') ,即1 == NaN ,当然这是false

The == operator uses ECMAScript's abstract equality algorithm which is quite complex. ==运算符使用ECMAScript的抽象相等算法 ,这非常复杂。 Its exact behavior depends on the types of each argument involved, and each step usually involves another invoking another ECMAScript function. 它的确切行为取决于所涉及的每个参数的类型,每个步骤通常涉及另一个调用另一个ECMAScript函数。

The if(condition) statement converts condition to a boolean using ECMAScript's ToBoolean which is simple enough to be expressed in a single table. if(condition)语句使用ECMAScript的ToBooleancondition转换为布尔值,该ToBoolean足够简单,可以在单个表中表示。 As you can see in the spec, any string is truthy (according to ToBoolean ) if it has a nonzero length. 正如您在规范中看到的,如果它具有非零长度,则任何字符串都是真实的(根据ToBoolean )。

true = boolean type true =布尔类型

'true' = string type 'true'=字符串类型

the expression "if ('true')" evaluates the 'true'(string) as true(boolean) on the same way that if('foo') or any other string does. 表达式“if('true')”以与if('foo')或任何其他字符串相同的方式将'true'(字符串)计算为true(boolean)。

A non-empty string will return true: 非空字符串将返回true:

  • if ('0') true; // true
  • if ('false') true; // true
  • if ('anything') true; // true

A null string will return undefined and thus is falsy : 空字符串将返回undefined,因此是假的

  • if ('') true; // not true

When comparing types, JavaScript will try to do some magic for you: 在比较类型时,JavaScript会尝试为您做一些魔术:

  • if (1 == "1") true; // true

But it fails when converting a string to boolean: 但是在将字符串转换为布尔值时失败:

  • if(true == "true") true; // not true

true is a boolean value 'true' is a string. true是布尔值'true'是字符串。

you are comparing different data types. 您正在比较不同的数据类型。 look here: http://w3schools.com/js/js_datatypes.asp 看这里:http://w3schools.com/js/js_datatypes.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM