Skip to content

Wrong Condition

Quick summary

Your condition evaluates to the wrong true/false value, so your code takes the wrong path or loops the wrong number of times.

Why this happens

  • Used = (assignment) instead of == or === (comparison)

  • Off-by-one in a boundary comparison (e.g., < vs <=)

  • Confused AND/OR logic or misplaced a negation

Minimal example

✗ Broken code
let x = 5;
if (x = 10) {
  console.log("ten");
}
✓ Fixed code
const x = 5;
if (x === 10) {
  console.log("ten");
}

= assigns 10 to x (and is always truthy), so the block always runs — === compares x to 10 correctly.

How to diagnose

  • Log the condition's value to see what it actually evaluates to

  • Test boundary cases (0, 1, the exact limit, one past the limit)

  • Check operator precedence — add parentheses to make intent explicit

How to fix

  • Use the correct comparison operator (=== in JS, == in Python)

  • Fix the boundary value or the comparison direction (< vs <=)

  • Add parentheses to clarify the order of logical operations

How to prevent

  • Use === (strict equality) in JavaScript to avoid type coercion bugs

  • Write unit tests for boundary values and edge cases

Related Resources

Related Glossary

Related Lessons

Related Practice

← Back to language