CodersTechZone
  • .NET
    • C#
    • ASP.Net
  • HTML
  • Javascript
  • CSS
  • Database
    • SQL Server
    • MYSql
    • Oracle
  • AI
  • TechNews
  • Web-Stories

What Happens When You Type console.log(1 < 2 < 3)? (JS Gotchas) ๐Ÿ˜ฒ

Shawpnendu Bikash Maloroy

March 8, 2025
JavaScript unexpected console.log result
Spread the love

The Surprising Result

Table of Contents

  • The Surprising Result
  • Understanding the Weird Behavior
    • Step 1: Evaluating Left to Right
    • Step 2: The Broken Logic in 3 > 2 > 1
  • How to Avoid This Mistake
    • Best Practice: Always Use Parentheses
  • Why Does This Matter in 2025? ๐Ÿš€
    • Common Pitfalls to Avoid
  • Final Thoughts ๐Ÿ’ก

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! ๐Ÿคฏ

JS Console.log true false mystery

๐Ÿ“Œ 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 to 1 (because true is coerced into a number in comparisons).
  • Now the expression is:
1 < 3

Which is true! โœ…

JavaScript Boolean conversion

๐Ÿ“Œ 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 to 1, so now we have:
1 > 1
  • Since 1 is not greater than 1, the result is false! โŒ
ExpressionEvaluates ToFinal Result
1 < 2 < 3true < 3 โ†’ 1 < 3true โœ…
3 > 2 > 1true > 1 โ†’ 1 > 1false โŒ

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 common JS programming pitfalls
  • 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.

How to handle value comparisons in JavaScript?

๐Ÿ“Œ 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! ๐Ÿ˜†

Shawpnendu Bikash Maloroy
Shawpnendu Bikash Maloroy

๐Ÿ‹๏ธโ€โ™‚๏ธ Discover Code Blocks From 20+ yrs JS Expert
๐Ÿ’ฅ Asp.net C# Developer
๐Ÿ† Solution Architect
๐Ÿ‘จโ€โœˆ๏ธ Database Administrator
๐Ÿ“ข Speaker
๐ŸŽ“ MCTS since 2009

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Spread the love
ยซPrevious
Nextยป

Good to Know

  • 7 Weird JavaScript Tricks That Look Like Bugs (#3 Will Shock You!)7 Weird JavaScript Tricks That Look Like Bugs
  • JavaScript Double Question Mark vs Or: A Comprehensive GuideJavaScript Double Question Mark vs Or - A Comprehensive Guide
  • 4 Powerful Ways to Compare Two Strings in JavaScriptCompare Two Strings in JavaScript
  • JavaScript Pass by Reference or Value: 4 Essential FactsJavaScript pass by reference or value with JS code example
  • JavaScript Round to 2 Decimal: 5 Secrets to Perfect Rounding!JavaScript Round to 2 Decimal 5 Secrets to Perfect Rounding! Image Credit: unspalsh (Ries Bosch)
  • 9 Hidden JavaScript Features You Probably Donโ€™t Know Exist ๐Ÿคฏ9 Hidden JavaScript Features

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • 7 Weird JavaScript Tricks That Look Like Bugs
    7 Weird JavaScript Tricks That Look Like Bugs (#3 Will Shock You!)
  • Build a JavaScript Gamer Journal in 8 Lines
    ๐ŸŽฎ Build a JavaScript Gamer Journal in 8 Lines: Track Your Wins Like a Pro! ๐Ÿ†
  • JavaScript Pet Feeder Reminder in 7 Lines
    How to Code a Simple JavaScript Pet Feeder Reminder in 7 Lines: Feed Your Pet Like a Coding Boss! ๐Ÿถ
  • 10-line calculator JavaScript
    Build a Simple Calculator in JavaScript: 10 Lines to Wow Your Friends!
  • JavaScript No-Code Automation
    JavaScript No-Code Automation: How to Replace Zapier with a Few Lines of Code

About-CodersTech Zone |  Contact |  Disclaimer |  Fact-Checking policy |  Affiliate Disclosure |  Privacy Policy

Copyright ยฉ 2024 CodersTechZone.com