3

In javascript, String(true) == "true" evaluates to true, and Boolean("true") == true evaluates to true, so why does "true" == true evaluate to false?

enter image description here

Dusty
  • 3,946
  • 2
  • 27
  • 41
  • 1
    not following your logic...why should it? – blockhead Jul 03 '17 at 19:58
  • 3
    Because JavaScript? – Heretic Monkey Jul 03 '17 at 19:58
  • 3
    maybe you get confused using `Boolean("true")`, since `Boolean("false")` also returns true. – Ulysse BN Jul 03 '17 at 19:59
  • Per the Boolean docs at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean): _"If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. If the DOM object document.all is passed as a parameter, the new boolean object also has an initial value of false. **All other values, including any object or the string "false", create an object with an initial value of true**."_ – j08691 Jul 03 '17 at 20:00
  • Answers on [What exactly is Type Coercion in JavaScript](https://stackoverflow.com/q/19915688/215552) could be helpful. – Heretic Monkey Jul 03 '17 at 20:00
  • 1
    Note, that you've thrown the result of the type coersion away. – Teemu Jul 03 '17 at 20:01
  • My understanding is that the coercion comparator should coerce the left side to the type of the right side, which seems like it should coerce to `true == true` – Dusty Jul 03 '17 at 20:06
  • @Dusty, that is a wrong assumption. The coercion in this case is to number. – trincot Jul 03 '17 at 20:09
  • Yeah, so nothing is ever coerced to Boolean! Who knew (you guys, I guess!) – Dusty Jul 03 '17 at 20:14

2 Answers2

9

In "true" == true the coercion is to number. So this becomes:

NaN == 1

... which is false.

The loose equality operands table on mozilla.org might be useful to check out.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Crazy. This matches Hennek's answer that Boolean gets converted to number, no matter which side of the comparator it's on, apparently? – Dusty Jul 03 '17 at 20:12
  • That conversion does not always happen for booleans (as the table on mozilla.org also shows), but in practical terms you can think of it that way: the outcome of the loose equality will be consistent with it. – trincot Jul 03 '17 at 20:23
2

Take a look to the MDN (Mozilla Developer Network). When we compare two operands of differents types, [the Abstract Equality Comparison Algorithm] will attempt to convert them to the same type before making the comparison.

Finally, the answer is the following:

If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.

Then

When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.

So, finally, we compare Nan == 1 which is false as pointed by @trincot.

lebol
  • 433
  • 5
  • 12