Avoiding Conditional
Bugs
Master the art of writing reliable conditional logic in JavaScript by understanding ordering pitfalls, boundary traps, and comparison gotchas.
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?
Anatomy of an if/else if Chain
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 \)
== 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
Example 1: Grade Calculator Bug
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
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
function canVote(age) { if (age > 18) { return true; } return false; }
function canVote(age) { if (age >= 18) { // >= includes 18 return true; } return false; }
Example 3: Type Coercion Trap
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"!
if (userInput === "" || userInput === null) { console.log("No input provided"); }
Example 4: Overlapping Ranges
function shippingCost(weight) { if (weight <= 5) return 5; if (weight <= 20) return 10; if (weight <= 10) return 8; // dead code! return 20; }
function shippingCost(weight) { if (weight <= 5) return 5; if (weight <= 10) return 8; if (weight <= 20) return 10; return 20; }
Common Mistakes
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
function classify(n) {
if (n > 0) return "positive";
if (n > 10) return "large positive";
if (n > 100) return "very large";
return "non-positive";
}
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
let x = "";
if (x == false) {
console.log("A");
}
if (x === false) {
console.log("B");
}
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?