5

Question is in the title. I've just tried to run next statements in Chrome console and got strange (as for me) result:

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
bsiamionau
  • 8,099
  • 4
  • 46
  • 73

5 Answers5

16

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').

So given this code true == 'true', it finds this:

"If Type(x) is Boolean, return the result of the comparison 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:

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

So now it's doing 1 == ToNumber('true'), which is 1 == NaN, which of course is false.

Blue Skies
  • 2,935
  • 16
  • 19
2

The == operator uses ECMAScript's abstract equality algorithm which is quite complex. Its exact behavior depends on the types of each argument involved, and each step usually involves another invoking another ECMAScript function.

The if(condition) statement converts condition to a boolean using ECMAScript's ToBoolean which is simple enough to be expressed in a single table. As you can see in the spec, any string is truthy (according to ToBoolean) if it has a nonzero length.

apsillers
  • 112,806
  • 17
  • 235
  • 239
0

true = boolean type

'true' = string type

the expression "if ('true')" evaluates the 'true'(string) as true(boolean) on the same way that if('foo') or any other string does.

Jacobson
  • 674
  • 5
  • 10
0

A non-empty string will return true:

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

A null string will return undefined and thus is falsy:

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

When comparing types, JavaScript will try to do some magic for you:

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

But it fails when converting a string to boolean:

  • if(true == "true") true; // not true
vol7ron
  • 40,809
  • 21
  • 119
  • 172
-3

true is a boolean value 'true' is a string.

you are comparing different data types. look here: http://w3schools.com/js/js_datatypes.asp

fifamaniac04
  • 2,343
  • 12
  • 49
  • 72
  • javascript converts the types when using == but not with === – Philipp Sander Oct 30 '13 at 18:21
  • oh please. lots of hate. But w3 really helped me back in the day to get going with javascript. They never tried to be "the spec". Just a straightforward way to get people going. In any school, you always oversimplify things for the sake of comprehension, then you present the "truth" later. – AwokeKnowing Oct 30 '13 at 18:21
  • @AwokeKnowing maybe but what do you think SO users expect? I can bet not "truth" in your meaning. MDN 100500 times better for referencing. At last they have own browser and JS engine and w3fools not. – antyrat Oct 30 '13 at 18:33
  • 1
    @antyrat: there was a time where w3fools had a point, but their biggest argument now is that w3schools isn't wikified and so isn't updated at supersonic speeds :) They do have a forum and suggestion methods, which they didn't before. Instead of asking them to wikify, another site should provide that option.... oh wait, others have tried and have been eclipsed by w3schools, which is often 95% accurate and you can google the other 5% or come to SO if you need help. – vol7ron Oct 30 '13 at 18:40