Evaluate AND, OR and NOT statements and build truth tables for compound conditions.
Why this matters
Every if statement in a program, every search filter, every safety interlock on a machine and every logic gate in a chip runs on the same small set of rules. Boolean logic, named after the mathematician George Boole, is how you reason precisely about conditions that are either true or false. Getting it right prevents bugs like "the alarm never fires" or "everyone can see the admin page".
The core idea
A proposition is a statement that is either true or false: "the tests pass", "the user is an admin", "the temperature is above 80 °C". We combine propositions with three basic operators:
| Operator | Meaning | Maths | JavaScript | Python |
|---|---|---|---|---|
| AND | both must be true | p ∧ q | p && q | p and q |
| OR | at least one is true | p ∨ q | p || q | p or q |
| NOT | flips true and false | ¬p | !p | not p |
OR in logic is inclusive: "p OR q" is true when both are true. Everyday English sometimes means "one or the other but not both"; that is a different operator called XOR (exclusive or).
Truth tables
A truth table lists every possible combination of inputs and the result. With two inputs there are 2 × 2 = 4 rows; with three inputs, 2³ = 8 rows.
| p | q | p AND q | p OR q | NOT p |
|---|---|---|---|---|
| T | T | T | T | F |
| T | F | F | T | F |
| F | T | F | T | T |
| F | F | F | F | T |
Read it row by row. AND is true in only one row; OR is false in only one row.
Worked example 1: a deploy rule
A team's rule: "deploy only if the tests pass AND the change is approved AND it is NOT Friday."
canDeploy = testsPass && approved && !isFriday
Three inputs means 8 rows. Only one row gives true: tests pass = T, approved = T, Friday = F. Writing the table out is a reliable way to confirm a rule says exactly what you meant.
Worked example 2: order of operations
Like arithmetic, logic has precedence: NOT first, then AND, then OR. So
a || b && c means a || (b && c), not (a || b) && c.
With a = T, b = F, c = F: the first is T || F = T; the second would be T && F = F. Different answers. Use parentheses whenever you combine AND and OR; readers (including future you) will thank you.
De Morgan's laws
Two rules let you push a NOT through parentheses:
- NOT (p AND q) = (NOT p) OR (NOT q)
- NOT (p OR q) = (NOT p) AND (NOT q)
Example: "it is not true that the site is up and fast" means "the site is down, or it is slow (or both)". These laws are handy for simplifying conditions in code:
// These two conditions are equivalent
if (!(isLoggedIn && isAdmin)) { /* deny */ }
if (!isLoggedIn || !isAdmin) { /* deny */ }
You can prove a law with a truth table: compute both sides for all four rows and confirm they match.
Building a truth table in code
const values = [true, false];
for (const p of values) {
for (const q of values) {
console.log(p, q, p && q, p || q);
}
}
Nested loops visit every combination, which is exactly what a truth table is.
From logic to hardware
Inside a processor, AND, OR and NOT are physical logic gates built from transistors. Combining gates gives circuits that add numbers, compare values and store bits. The same truth tables describe both your if statements and the chip running them.
Common mistakes
- Treating OR as exclusive. Inclusive OR is true when both inputs are true.
- Mixing AND and OR without parentheses, then relying on precedence by accident.
- Negating a compound condition incorrectly, such as writing
!a && !bwhen you meant!(a && b). - Using
=(assignment) instead of===(comparison) in a JavaScript condition.
Quick recap
- Propositions are true or false; AND, OR and NOT combine them.
- A truth table lists every input combination: 2ⁿ rows for n inputs.
- Precedence: NOT, then AND, then OR. Use parentheses anyway.
- De Morgan: NOT(p AND q) = NOT p OR NOT q, and NOT(p OR q) = NOT p AND NOT q.
Try it
In the Do tab, write the deploy rule as a JavaScript function. A helper that builds the full truth table is already written so you can see all 8 rows.