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/ORlogic or misplaced a negation
Minimal example
let x = 5;
if (x = 10) {
console.log("ten");
}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 bugsWrite unit tests for boundary values and edge cases
Related Resources
Related Lessons
- Conditions
Learn how conditional logic works
Related Practice
- Predict the typeof Output
Practice predicting how values evaluate.