(true + false) > 2 + true; Why does this return false?

13,657

Solution 1

true equals 1. false equals 0.

So your expression is equivalent to:

(1 + 0) > 2 + 1

which reduces to

1 > 3

which is false!

Solution 2

That's because your code evaluates to :

1 + 0 > 2 + 1

Which is equivalent to :

1 > 3

This is due to the way that Javascript is evaluated and interpreted by the Javascript engine when you are using arithmetic operators on some types, such as booleans, which are implicitely converted, in this case, to numbers.

Share:
13,657
Julio Marins
Author by

Julio Marins

I graduated at CEFET/RJ as a programmer and since than never stopped

Updated on June 04, 2022

Comments

  • Julio Marins
    Julio Marins almost 2 years

    Im studying javascript and can't figure it out why this line returns false:

    (true + false) > 2 + true