Spot the De Morgan's Law Bug
Question
This code should print "Invalid" when age is NOT (18 or older AND has license), but the logic is wrong. What is the issue?
Code
let age = 16;
let hasLicense = false;
if (!(age >= 18) && !hasLicense) {
console.log("Invalid");
}
Hint
De Morgan's Law: NOT (A AND B) = (NOT A) OR (NOT B).
Explanation
By De Morgan's Law, !(A && B) is equivalent to !A || !B, not !A && !B. The code uses && instead of ||. With age=16 and hasLicense=false: !(16>=18) is true, !false is true, so true && true = true — it happens to work here, but consider age=16, hasLicense=true: the original !(age>=18 && hasLicense) = !(false) = true (invalid), but !age>=18 && !hasLicense = true && false = false (misses it). The fix is if (!(age >= 18) || !hasLicense).
Related Resources
Related Lessons
- Boolean Logic
Learn about boolean logic and operators.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.