Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why is 3 > 2 > 1 false in JavaScript?

Do you know what will be the output of this code?

JavaScript
console.log(3 > 2 > 1);
Show answer and explanation

While this is a perfectly valid mathematical comparison, which would usually yield true, programming languages evaluate expressions differently, and JavaScript itself is no different.

We have to split the expression into two parts: first of all, 3 > 2 gets evaluated, which yields true.

3 > 2 === true

Now the rest has to be evaluated: true > 1. This isn’t directly possible, as the types are different so coercion happens under the hood. In case of the greater-than (>) operator, a type hint of number is passed so true is converted to a number.

Number(true) > Number(1)
=> 1 > 1
=> false
Take the full quiz Next question →

More Type Coercion questions

  1. Why does ('b' + 'a' + + 'a' + 'a') return "banana"?
  2. Why does 1 + "2" + "2" return "122"?
  3. Why is false == '0' true in JavaScript?
  4. What do true == [] and true == ![] return?
  5. Why does [] + {} return "[object Object]"?

Related topics