The named logic laws

LLM-authored, human-reviewed

Proofs & logic

The Logic exercises on this site name four laws and one operation worth knowing by name: De Morgan’s law, modus ponens, the contrapositive, the law of excluded middle, and exclusive or (XOR). If you never took a logic course, the names read like inside references. Here is what each one actually says, in JavaScript.

De Morgan’s law

De Morgan’s law is the one you already use. Negating an AND flips it into an OR of negations, and negating an OR flips it into an AND of negations.

!(a && b) === !a || !b
!(a || b) === !a && !b

You reach for this every time you invert a compound condition. “Show the error when the user is not both logged in and active” becomes if (!isLoggedIn || !isActive). The law is the reason that rewrite is safe. It is also the reason the two spellings read differently: “not (both)” is “at least one is missing”, while “not (either)” is “both are missing”. Same claim, two phrasings.

Exclusive or (XOR)

Exclusive or is the “one or the other, not both” operation, built from the three operators above. It is true exactly when its two operands differ.

(a || b) && !(a && b) // true when exactly one side is true
a ^ b // bitwise XOR in JS

Ordinary || is inclusive: true when either or both are true. XOR rules out the both case, which is what “exclusive” means. It is the light-switch toggle, the parity check, and the operation behind the bitwise ^. The recall drills spell it out as (a || b) && !(a && b) and ask you to evaluate it under an assignment; that formula is just XOR written with the operators you already know, and evaluating it is substitution like everything else here.

Modus ponens

Modus ponens is the if-then you run without thinking.

(a -> b) && a   implies   b

If A implies B, and A holds, then B holds. In code: “if the user is an admin, then they can edit. The user is an admin. So they can edit.” The rule has a name because formal logic names everything, but it is the least exotic item on this list. You have been applying it since your first if statement.

The contrapositive (the one that trips people up)

The contrapositive is the surprising one. A conditional a -> b is equivalent to !b -> !a: flip the two sides and negate both.

(a -> b) === (!b -> !a)

“Rain implies a wet street” is the same claim as “a dry street implies no rain.” Both describe the same world; the second is the first read backward.

The trap is the converse, b -> a, which is not equivalent. “A wet street implies rain” is false: the street can be wet because a pipe burst. The converse and the contrapositive both swap the two sides; only the contrapositive also negates them, and that negation is the entire difference. If you remember one thing from this article, remember that. Swapping is not enough. The match-pairs exercise on this site uses the converse as its distractor because confusing the two is the classic error.

Law of excluded middle

The law of excluded middle says a statement is either true or false. There is no third value.

a || !a   // always true

This is the rule that makes truth tables work at all. Each variable can only be true or false, so a table over two variables has exactly four rows, and checking all four rows settles every question about the formula. It is also the rule JavaScript quietly bends with null, undefined, and NaN: null is not true and not false, so the excluded middle is a claim about your booleans, not about every value the language lets you put in a variable.

Why learn the names

The names are a shortcut. When you recognize “this is contrapositive reasoning”, you can rewrite a condition confidently instead of re-checking a truth table from scratch. When you see !(a && b) and want !a || !b, you are applying De Morgan without naming it. The names are how the Logic exercises and the recall drills talk about these moves, and how formal computer science talks about them too: proofs, type systems, and static analysis all lean on the same vocabulary.

The formulas are checkable by truth table. The names are memorization. Learn the names because they are the handle the rest of the material grabs onto, not because they carry meaning beyond the formula they label.

Where this site takes it

Related exercises

← Back to articles