Skip to content

De Morgan's Law Misapplication

Quick summary

You negated !(a && b) as !a && !b instead of !a || !b — the condition fires on the wrong inputs.

Why this happens

  • Distributing ! over && as && instead of || (and vice versa)

  • Forgetting to negate each operand when pushing ! inward

  • Mixing up 'neither' (&&) with 'not both' (||) semantics

Minimal example

✗ Broken code
const canEdit = user.isAdmin && user.isActive;
const cannotEdit = !user.isAdmin && !user.isActive; // wrong — should be OR
console.log(cannotEdit); // true only when BOTH are false
✓ Fixed code
const canEdit = user.isAdmin && user.isActive;
const cannotEdit = !user.isAdmin || !user.isActive; // De Morgan
console.log(cannotEdit); // true when either is false

!(isAdmin && isActive) is !isAdmin || !isActive, not !isAdmin && !isActive. The wrong form only fires when both are false, missing the cases where just one is false.

How to diagnose

  • Write a truth table for the original and the negated expression

  • Test with all combinations of true/false for the operands

  • Compare the negated form with the canonical De Morgan result

How to fix

  • Apply De Morgan correctly: !(a && b) === !a || !b, !(a || b) === !a && !b

  • Keep the negation outside and test the positive form to avoid mistakes

  • Use named boolean variables so the intent is clear

How to prevent

  • Write a truth-table test for non-trivial boolean logic

  • Name intermediate boolean results to make intent explicit

Related Resources

Related Glossary

Related Lessons

← Back to language