Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 1003 Bytes

13.Operator-precedence.md

File metadata and controls

45 lines (31 loc) · 1003 Bytes

13. Operator precedence

Problem

https://bigfrontend.dev/quiz/operator-precedence

Problem Description

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);

Answer

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

Explanation

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.

Reference

Operator precedence: Example