Truth tables: the logic your code already runs on

LLM-authored, human-reviewed

Proofs & logic

Every conditional you write is a logic puzzle. if (a && b), if (!user.blocked || admin), and condition ? result : fallback are formulas over true and false. Each has one exact answer for every possible input. You usually reason about them informally, and that is usually enough. A truth table gives you the machine-readable answer: one row for each possibility, with the formula’s result. It powers the logic exercises on this site.

What a truth table is

A truth table lists every assignment of true and false to the variables, then shows the formula’s result for each assignment. Two variables produce four possibilities, so a && b gets four rows:

a b a && b
false false false
true false false
false true false
true true true

That is the whole table. Four rows. One assignment per row. The last column holds the formula’s output. Nothing remains for intuition: every input appears, and every output is computed.

Row order is a convention. This site uses bit order, with the first variable changing fastest: a=F,b=F, a=T,b=F, a=F,b=T, a=T,b=T - the order produced by counting in binary. The truth-table exercises ask for the output column from top to bottom, and the grader checks that exact order.

The five connectives, as code

The logic exercises use five connectives. You already know the code spelling for each one. (Math writes these same five as , , ¬, , - same ideas, a different font; the mathematical shorthand article lists both spellings side by side.)

  • And, a && b - true only when both sides are true. One false input makes the whole conjunction false.
  • Or, a || b - false only when both sides are false. One true input makes the whole disjunction true.
  • Not, !a - flips the value.
  • Implication, a -> b - false in exactly one row: true premise, false conclusion.
  • Biconditional, a <-> b - true exactly when both sides have the same value.

The first three are familiar. The last two cause the mistakes, so the site gives them dedicated exercises.

The connective that surprises: implication

a -> b is the promise “if a, then b”. Its table has exactly one false row:

a b a -> b
false false true
true false false
false true true
true true true

Three rows are true and one is false. The false row is the only one that matches the naive reading. The first row causes the surprise: when the premise is false, the implication is true. In JavaScript, the mapping is a conditional:

const implies = (a, b) => a ? b : true;

a ? b : true says exactly what the table says. When a is true, the answer is b; when a is false, the answer is true. This is called vacuous truth, and it is the single most counterintuitive row in propositional logic. “If it rains, I stay home” is true on a sunny day - not because anything was checked, but because a promise whose condition never fires is a promise kept by default.

The site’s implication exercises make this explicit. Evaluate false -> true (true, the row that surprises everyone), evaluate true -> false (false, the one failing row), and evaluate chains like !a -> b where the antecedent is itself a compound. Each exercise is one row of this table in disguise.

The biconditional is equality

a <-> b says “the same value”. Its table is true on the two rows where both sides agree and false on the two where they differ:

a b a <-> b
false false true
true false false
false true false
true true true

In JavaScript, the mapping is the equality operator:

const iff = (a, b) => a === b;

a === b is exactly the biconditional. This is not a coincidence of syntax: propositional logic’s iff and programming’s equality are the same idea - two values that are either the same or not, with nothing in between. When the site’s exercises ask you to evaluate a <-> b under an assignment, you are computing an ===; when they ask for its truth table, they are asking for the four rows of the equality function.

Why tables settle “are these the same?”

A truth table answers the equivalence question. Two formulas are logically equivalent when they produce the same output on every row - their tables match, column by columnbackhouse. This is an exhaustive check. Do not ask whether they feel the same; check whether every possible input gives the same answer.

De Morgan’s law is the classic case:

a b !(a && b) !a || !b
false false true true
true false true true
false true true true
true true false false

The two columns match on every row, so the formulas are equivalent - “not (a and b)” is the same as “not a or not b”. De Morgan’s laws are, in programmer terms, the logic of “wait, did I say that?” - the same claim in two spellings. Use the table to settle which spelling you meant. The equivalence exercises on this site do exactly that: they compare two formulas row by row.

Two more table shapes matter:

  • Tautology - the output column is all true on every row. a || !a is a tautology: whatever a is, one of the two sides is true. a -> a is a tautology for the same reason.
  • Contradiction - the output column is all false on every row. a && !a can never be true.

The tautology exercises are the all-true column; the truth-value exercises are single rows; the truth-table exercises are the whole column.

Where tables stop

A table grows by a factor of two per variable: one variable, two rows; two variables, four rows; three, eight; and so on. The site’s logic exercises cap at eight variables - 256 rows, still enumerable, still checkable by hand or by machine. Beyond that, the table stops being practical. Proving a claim about an unbounded domain - every natural number, every list - is what the proofs and induction exercises are for. The quantifier exercises draw the same line from the other side: when the domain is finite, enumeration decides; when it is infinite, testing can only refute, and proving is a different craft. The truth table is the finite case done honestly.

Why this matters here

The logic exercises are a truth-table machine wearing different costumes. The truth_table exercises ask for the output column of a formula - the table itself. The truth_value exercises ask for one row: substitute the assignment, compute the connective. The equivalence exercises ask whether two formulas’ columns match on every row. The tautology exercises ask whether a column is all true. Every exercise’s JavaScript bridge shows you the same connective in the syntax you write every day - &&, ||, !, the ternary as implication, === as the biconditional.

A truth table gives the informal reasoning you already do a precise form. “This looks wrong” becomes “the row a=true, b=false disagrees”. “I think these are the same” becomes “their columns match on all four rows”. The table is not a detour into math - it is the exhaustive version of the check you were already doing by hand, one row at a time.

Where to go next


  1. Roland Backhouse, Algorithmic Problem Solving. Wiley, 2011 - the source that drills exactly this move: decide a proposed equivalence by evaluating both sides on every assignment, not by staring at the formulas.

Related exercises

← Back to articles