0

Short version of the question

true == 'true' //evaluates to false

Why?

Full version

OK, I know that using == instead of === is a bad practice, but this question was in the FrontendLeague quiz today and I was very surprised with the answer.

My logic was quite straightforward (all the following statements evaluate to true):

Boolean("true") === true
String(true) === 'true'
'true' ? true : false === true

Nevertheless:

'true' == true // false
true == 'true' // also false

As it was stated by Kyle Simpsons in this great video, most of wtfjs'es aren't actually so wtf and are fully alright according to js specs.

So, could you please explain why 'true' == true is false?

Thanks!

P.S.

I'm sorry if this question was already asked, but it's pretty hard to find smth by keywords javascript and true (wtfjs gives nothing).

Best regards, Alexander

UPD

Sorry for duplicate, the complete answer can be found here, as mentioned in the comments by Joachim Rohde.

Community
  • 1
  • 1
Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56
  • 1
    Duplicate: http://stackoverflow.com/questions/21041619/why-does-not-the-true-match-true-with-double-equals-sign-in-javascript – Joachim Rohde Nov 19 '15 at 20:12
  • `'true'` is a string, and not the same thing as a boolean value, `true`. `Boolean("true") === true` because the "truthiness" of `"true"` is `true`. Same for `'true' ? true : false === true`. JS evidently converts `true` to the string `'true'` when you apply `String(true)`. But ultimately, `true` and `'true'` are NOT the same thing. The first is a boolean value, the second is a string. – lurker Nov 19 '15 at 20:14
  • @lurker I understand that, but ``==`` was used, not ``===``. – Alexander Mikhalchenko Nov 19 '15 at 20:15
  • @AlexanderM. Have you read the answer at the linked question? – Barmar Nov 19 '15 at 20:16
  • @Barmar yes, thanks, that's what I was looking for but haven't found :) – Alexander Mikhalchenko Nov 19 '15 at 20:19
  • If `'true' == true` is false, then JS isn't converting `'true'` as a string to a boolean value based upon its truthiness. You'll need to dig into the JS documentation regarding implicit type conversion to determine what it does here before the actual comparison. – lurker Nov 19 '15 at 20:20
  • Reading the ES5 documentation you can read this on section 11.9.3: If `Type(x) is String`, then return `true` if `x` and `y` are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, `return false`. – abaracedo Nov 19 '15 at 20:23
  • 1
    Javascript uses `Abstract Equality Comparison Algorithm` and according to it if one of the operands is boolean then it is first converted to number (i.e. true -> 1 and false -> 0) and then compared. Read the 6th point http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 – Zohaib Ijaz Nov 19 '15 at 20:26
  • @ZohaibIjaz would you care to post your comment as an answer? (because it's actually the bast answer on this question). – Alexander Mikhalchenko Nov 19 '15 at 21:46
  • I would like to post it as an answer but this question has been marked as `duplicate`. If you want to encourage me, vote up for above comment.. and Thanks for your appreciation... – Zohaib Ijaz Nov 20 '15 at 07:32

0 Answers0