The Surprising Result
Table of Contents
Imagine you’re coding and you type the following in JavaScript:
console.log(1 < 2 < 3);
What do you expect? true? That makes sense because 1 is less than 2, and 2 is less than 3. But JavaScript surprises us!
๐ฅ The output is true
!
But now, try this:
console.log(3 > 2 > 1);
What do you think? Maybe true
again? Nope! The output is false! ๐คฏ

๐ Tip:
When in doubt, break expressions into smaller parts and log intermediate values to understand JavaScript behavior.
Understanding the Weird Behavior
Step 1: Evaluating Left to Right
JavaScript evaluates expressions from left to right. Let’s check the operator precedence for the following expression:
Mentioning again, Let’s analyze 1 < 2 < 3
step by step:
- First, JavaScript evaluates
1 < 2
, which is true. - Now, the expression becomes:
true < 3
- JavaScript converts
true
to1
(becausetrue
is coerced into a number in comparisons). - Now the expression is:
1 < 3
Which is true! โ

๐ Tip:
JavaScript automatically converts true
to 1
and false
to 0
in numeric operations. Keep this in mind to avoid unexpected results.
Step 2: The Broken Logic in 3 > 2 > 1
Now, letโs analyze 3 > 2 > 1
step by step:
- First,
3 > 2
is true. - Now, the expression becomes:
true > 1
- JavaScript converts
true
to1
, so now we have:
1 > 1
- Since
1
is not greater than1
, the result is false! โ
Expression | Evaluates To | Final Result |
---|---|---|
1 < 2 < 3 | true < 3 โ 1 < 3 | true โ |
3 > 2 > 1 | true > 1 โ 1 > 1 | false โ |
How to Avoid This Mistake
If you want to compare three numbers correctly, use logical AND (&&
****) instead:
console.log(1 < 2 && 2 < 3); // true โ
console.log(3 > 2 && 2 > 1); // true โ
This ensures that both conditions are evaluated separately instead of relying on JavaScriptโs type coercion.
๐ Tip:
Always use &&
when comparing multiple values to make your code clearer and more reliable.
Best Practice: Always Use Parentheses
To make sure your code behaves as expected, wrap comparisons in parentheses:
console.log((1 < 2) < 3); // true โ
console.log((3 > 2) > 1); // false โ
Parentheses make the evaluation clearer and predictable.
Why Does This Matter in 2025? ๐
Understanding JavaScript quirks is essential for writing bug-free code, especially as JavaScript continues to dominate web development. With AI-driven code assistants and automated testing, understanding language quirks helps developers debug more effectively.
Common Pitfalls to Avoid

- Avoid implicit type conversion by using strict equality (
===
) instead of loose equality (==
). - Use parentheses to make sure comparisons are evaluated the way you expect.
- Use
&&
for multiple comparisons instead of chaining comparison operators.
Final Thoughts ๐ก
JavaScript is powerful but tricky! The way it coerces values can lead to unexpected results. Always be careful with chained comparisons and use &&
or parentheses to avoid logic errors.

๐ Tip:
When debugging, use console.log()
at each step to see how JavaScript evaluates expressions.
Another way to avoid confusion is to use strict comparisons and avoid type coercion whenever possible. If your project requires strict logic, consider TypeScript or ESLint rules to catch potential issues early.
๐ Tip:
Use ===
instead of ==
whenever possible. It prevents unintended type coercion and ensures predictable results.
Understanding JavaScript’s quirks will make you a better developer. Keep practicing, testing, and experimenting!
๐ Did this trick surprise you? Try experimenting with other comparisons and see what happens!
๐ Next Steps: Share this gotcha with your friends and test them! ๐
๐๏ธโโ๏ธ Discover Code Blocks From 20+ yrs JS Expert
๐ฅ Asp.net C# Developer
๐ Solution Architect
๐จโโ๏ธ Database Administrator
๐ข Speaker
๐ MCTS since 2009
Leave a Reply