Leaderboard
Javascript May 1, 2026
Javascript Chained Comparison Gotcha

JavaScript Type Coercion — What does this output?

This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of Chained Comparison Gotcha and improve your technical interview readiness.

console.log(10 > 9 > 8)
A true
B false
C TypeError
D NaN

Detailed Explanation

Why This Question Matters

If you’re new to JavaScript, you probably look at 10 > 9 > 8 and think, "Obviously, this is true. 10 is bigger than 9, and 9 is bigger than 8." It feels like basic math. But in JavaScript, that’s not how it works.

This is a classic "gotcha" question. It tests your understanding of operator precedence and type coercion. Most juniors fail this because they read the code as a mathematical expression rather than a series of sequential operations. Understanding why this returns false is the difference between writing code that "just works" and actually knowing what your engine is doing under the hood.

Understanding the Code

To figure out what console.log(10 > 9 > 8) does, we have to stop reading it from left to right as a single phrase and start looking at it as a set of steps.

In JavaScript, the greater-than operator (>) has left-to-right associativity. This means the engine evaluates the first comparison before it even looks at the second one.

Here is the internal breakdown:

1. The first operation: The engine sees 10 > 9. This is a straightforward comparison. Since 10 is indeed greater than 9, this expression evaluates to true.
2. The state change: Now, the expression effectively becomes true > 8.
3. The coercion: Here is where things get weird. You can't logically compare a boolean (true) to a number (8). To fix this, JavaScript performs type coercion. It forces the boolean into a number so the comparison can happen. In JavaScript, true is coerced to 1 and false is coerced to 0.
4. The final operation: The expression is now 1 > 8. Since 1 is definitely not greater than 8, the final result is false.

Finding the Correct Answer

The correct answer is Option B: false.

If you guessed true, you were thinking in terms of algebra. In a math class, $10 > 9 > 8$ is a valid chain. In JavaScript, it's two separate operations.

Let's look at why other assumptions fail:
- "It should be true": This assumes JS handles "chained comparisons" automatically. It doesn't.
- "It should throw an error": You might think comparing a boolean to a number would crash the program. In a stricter language like Java or Swift, it would. But JavaScript is dynamically typed and loves to coerce values to keep the script running, even if the result is logically nonsensical.

Common Mistakes Developers Make

The biggest mistake is assuming JavaScript "understands" the intent of the code.

Beginners often run into this when trying to check if a number falls within a specific range. You'll see code like this in a lot of junior PRs:

// ❌ Wrong way to check if age is between 18 and 30
if (18 < age < 30) { 
  // This will not work as expected!
}

If age is 25, the engine does this:
1. 18 < 25 $\rightarrow$ true
2. true < 30 $\rightarrow$ 1 < 30 $\rightarrow$ true

Wait, that worked! But what if age is 10?
1. 18 < 10 $\rightarrow$ false
2. false < 30 $\rightarrow$ 0 < 30 $\rightarrow$ true

Suddenly, a 10-year-old passes a check for being at least 18. This is a dangerous bug because it doesn't throw an error; it just gives you the wrong data.

Real-World Usage

In a professional production environment, you will almost never see 10 > 9 > 8 because it's useless. However, the *concept* of coercion and precedence is everywhere.

You'll encounter this when dealing with API responses where a value might come back as a string "10" instead of a number 10. If you start chaining comparisons or mixing types, JavaScript will start guessing what you want, and that's when bugs crawl into your codebase.

The professional way to handle range checks or multiple comparisons is to use the logical AND (&&) operator:

// ✅ The right way
if (age > 18 && age < 30) {
  // Now this actually works
}

This is explicit, readable, and doesn't rely on the engine's quirky coercion rules.

Key Takeaways

- JavaScript doesn't do chained comparisons like math does. It evaluates them one by one from left to right.
- Booleans are numbers in disguise. When forced into a numeric comparison, true becomes 1 and false becomes 0.
- Coercion is a double-edged sword. It prevents your app from crashing, but it can introduce silent logic errors that are nightmares to debug.
- Be explicit. If you need to check multiple conditions, use &&. Don't trust the engine to guess your mathematical intent.

Why this matters

Understanding Chained Comparison Gotcha is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.

📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.

Ready for your shot?

Join thousands of developers solving one logic puzzle every morning.

Solve Today's Challenge →