Skip to content

Incorrect Comparison Operator

Quick summary

You used the wrong comparison operator, so a boundary value is included or excluded when it should not be.

Why this happens

  • Using > when you need >= (or vice versa)

  • Using < when you need <= (or vice versa)

  • Confusing the direction of the comparison

Minimal example

✗ Broken code
function canVote(age) {
  return age > 18; // 18-year-olds cannot vote!
}
console.log(canVote(18)); // false — should be true
✓ Fixed code
function canVote(age) {
  return age >= 18; // 18-year-olds can vote
}
console.log(canVote(18)); // true

Using > 18 excludes 18-year-olds, who should be able to vote. Changing to >= 18 includes the boundary value, giving the correct result.

How to diagnose

  • Test the exact boundary value to see if it is handled correctly

  • Print the condition result for boundary values

  • Review the comparison operator against the intended logic

How to fix

  • Change > to >= or < to <= as needed

  • Test with boundary values to verify the fix

  • Add comments explaining the intended boundary behavior

How to prevent

  • Always test boundary values in unit tests

  • Think carefully about whether the boundary should be inclusive or exclusive

Related Resources

Related Glossary

Related Lessons

← Back to language