01 / 20

Avoiding Conditional
Bugs

Master the art of writing reliable conditional logic in JavaScript by understanding ordering pitfalls, boundary traps, and comparison gotchas.

JavaScript

What Are Conditional Bugs?

A conditional bug is a logic error in an if, else if, or switch statement that causes your program to take the wrong branch. The code runs without crashing, but it silently produces incorrect results.

Why Do Conditionals Break?

Ordering
Branches checked in the wrong sequence, so a broader condition catches values first.
Boundaries
Off-by-one errors at thresholds: using \( < \) when you need \( \le \), or vice versa.
Comparison
Using == instead of ===, or confusing = (assignment) with === (equality).

Anatomy of an if/else if Chain

Check condition 1 false true Block 1 Check condition 2 false true Block 2 Check condition 3 false true Block 3 else (default) Order matters! First match wins

The Ordering Rule

When multiple conditions can be true for the same input, you must order branches from most specific to least specific. If a broad condition appears first, it will "swallow" inputs that should have matched a more specific branch below.

Boundary Conditions: \( < \) vs \( \le \)

\(x < 5\) 3 4 5 6 excluded \(x \le 5\) 3 4 5 6 included

== vs ===: The Critical Difference

== (loose) === (strict)
Type coercion Yes: converts types first No: types must match
"5" and 5 true false
0 and false true false
"" and 0 true false
null and undefined true false
Recommendation Avoid Always use

The Bug-Proofing Checklist

1
Use strict equality. Replace every == with === and every != with !==.
2
Order branches narrowest first. Place the most specific condition at the top of your if/else if chain.
3
Test the boundary. For every comparison, mentally plug in the exact threshold value and verify which branch runs.
4
Test edge cases. Check \( 0 \), empty strings "", null, undefined, and negative numbers.
5
Cover the default. Always include a final else block to handle unexpected inputs.

Example 1: Grade Calculator Bug

✕ Buggy Code
function getGrade(score) {
  if (score >= 70)  return "C";
  if (score >= 80)  return "B";  // never reached for 80+
  if (score >= 90)  return "A";  // never reached for 90+
  return "F";
}

Example 1: The Fix

✓ Fixed Code
function getGrade(score) {
  if (score >= 90)  return "A";  // narrowest first
  if (score >= 80)  return "B";
  if (score >= 70)  return "C";  // broadest last
  return "F";
}

Example 2: Off-By-One Boundary Bug

✕ Buggy Code
function canVote(age) {
  if (age > 18) {
    return true;
  }
  return false;
}
✓ Fixed Code
function canVote(age) {
  if (age >= 18) {    // >= includes 18
    return true;
  }
  return false;
}

Example 3: Type Coercion Trap

✕ Buggy Code
let userInput = "0";  // from a text field

if (userInput == false) {
  console.log("No input provided");
}
// Logs "No input provided" even though
// the user typed "0"!
✓ Fixed Code
if (userInput === "" || userInput === null) {
  console.log("No input provided");
}

Example 4: Overlapping Ranges

✕ Buggy Code
function shippingCost(weight) {
  if (weight <= 5)  return 5;
  if (weight <= 20) return 10;
  if (weight <= 10) return 8;  // dead code!
  return 20;
}
✓ Fixed: Order from narrowest to broadest
function shippingCost(weight) {
  if (weight <= 5)  return 5;
  if (weight <= 10) return 8;
  if (weight <= 20) return 10;
  return 20;
}

Common Mistakes

✕ Assignment in Condition
Writing if (x = 5) instead of if (x === 5). The single = assigns 5 to x, which is always truthy.
✕ Missing else
Forgetting the final else block. If no condition matches, the function returns undefined silently.
✕ Forgetting Falsy Values
Treating 0, "", and null as equivalent. Each is falsy but means something different logically.

Bug Type Summary

Bug Type What Goes Wrong How to Fix
Ordering Broad condition runs before specific one Narrowest first
Boundary Threshold value goes to wrong branch Test exact boundary
Comparison Type coercion creates false matches Use === always
Assignment = used instead of === in condition Linter / code review
Dead code Branch can never be reached Trace every path

Question 1 of 5

What will classify(50) return?

Question 2 of 5

Which operator should you always use for equality checks in JavaScript to avoid type coercion bugs?

Question 3 of 5

A function checks \( \text{age} > 18 \) to determine voting eligibility. What happens when age is exactly 18?

Question 4 of 5

What is logged to the console?

Question 5 of 5

When writing an if / else if chain to categorize values into ranges, what is the correct ordering strategy?