Common Bugs with Operators

📌 Common bugs · operators✍️ Written by Tom Reyes📅 Reviewed 2026-04-08⏱ ~7 min read

Five operator traps that every beginner falls into at least once.

Bee mascot
From Bee: "I see this exact bug in beginners every week. Read it once, and you'll spot it on yourself before it bites."
#1

= 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.
#2

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.
#3

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.
#4

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.
#5

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.

T
Tom Reyes
Reviewer · 12 yrs Java/JVM

Tom spent eight years as a backend engineer in fintech (Java + Kotlin) and four as a lead at an enterprise SaaS company. He reviews every Java example on this site and writes the data-structure deep dives. He cares deeply that beginners aren't taught bad habits they'll have to unlearn. More about Tom →