Common Bugs with Operators
Five operator traps that every beginner falls into at least once.
From Bee: "I see this exact bug in beginners every week. Read it once, and you'll spot it on yourself before it bites."
= vs ==
js
if (loggedIn = true) { ... } // always truthy⚠️
Why this happens
Single = assigns; double = compares.✅
The fix
Use === in JS, == in Python/Java. Linters catch this.Operator precedence surprise
js
const ok = isAdmin || isOwner && isActive;⚠️
Why this happens
&& binds tighter than ||. This is "isAdmin OR (isOwner AND isActive)" — probably not what you wanted.✅
The fix
When mixing operators, add parentheses: (isAdmin || isOwner) && isActive.Modulo with negative numbers
js
console.log(-1 % 3); // -1 in JS, 2 in Python⚠️
Why this happens
Different languages handle negative modulo differently. JS preserves sign of dividend; Python preserves sign of divisor.✅
The fix
Normalize: ((n % m) + m) % m guarantees non-negative.Bitwise vs logical
js
if (flags & FLAG_A) { ... } // bitwise: returns number
if (a && b) { ... } // logical: returns boolean⚠️
Why this happens
Single-character & is bitwise — usually wrong in conditional logic.✅
The fix
Use && / || for conditions; & / | for bit manipulation.Short-circuit changing what runs
js
const ok = check() && save(); // save() only runs if check() is truthy⚠️
Why this happens
Side effects after && or || only run conditionally. Easy to miss in code review.✅
The fix
For clarity, hoist side-effecting calls into their own statements.💡
Want the full lesson?
Read Operators — the complete lesson, or test yourself with the quiz.