0

I read this in Eloquent JavaScript book:

The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ("") count as false, while all the other values count as true.

Because of this, expressions like 0 == false and "" == false are true.

And also following these rules this expression should evaluate to true:

console.log("A" == true)

But it evaluates to false. Why?

Moaaz Bhnas
  • 1,010
  • 7
  • 18
  • 36
  • 1
    You are not actually casting string `"A"` to boolean; you're comparing a string and a boolean, which is a different thing and it's definitively not solved by casting the left operand to the type of the right operand. If you check the [accepted answer](https://stackoverflow.com/a/28571570/13508) in the linked question, I reckon that rule #10 applies. – Álvaro González Dec 05 '17 at 15:36
  • 1
    @ÁlvaroGonzález: Actually, rule 7 applies: `x == ToNumber(y)` > `"A" == 1` , which is `false`, of course. – Cerbrus Dec 05 '17 at 15:42
  • 2
    @Cerbrus Oops, yes, you're right, I overlooked that. – Álvaro González Dec 05 '17 at 15:43
  • Now I get it. It's not about truthy and falsy. Thanks guys @ÁlvaroGonzález Cerbrus – Moaaz Bhnas Dec 05 '17 at 15:52

0 Answers0