https://bigfrontend.dev/quiz/operator-precedence
What does the code snippet below output by console.log
?
console.log(0 == 1 == 2);
console.log(2 == 1 == 0);
console.log(0 < 1 < 2);
console.log(1 < 2 < 3);
console.log(2 > 1 > 0);
console.log(3 > 2 > 1);
console.log(0 == 1 == 2); // false
console.log(2 == 1 == 0); // true
console.log(0 < 1 < 2); // true
console.log(1 < 2 < 3); // true
console.log(2 > 1 > 0); // true
console.log(3 > 2 > 1); // false
0 == 1 == 2
returns false
, because JavaScript first evaluates 0 == 1
and the result is false
, then false
is converted to 0
by ==
operator and compared to 2
. The same logic applies to all the other cases.