Why This Matters
When you write a complex boolean condition with multiple variables, how do you know it covers every case correctly? A truth table is the answer. It systematically lists every possible combination of input values and shows the result of the expression for each one. This is the most reliable way to verify that your logic is correct before you ship it.
Truth tables let you see conjunction (AND) and disjunction (OR) in action across all inputs. They reveal edge cases that manual reasoning misses: what happens when all inputs are false? When exactly one is true? When you can build a truth table for any boolean expression, you gain the ability to prove correctness rather than guess at it.
In hardware design, truth tables define the behavior of logic gates and circuits. In software, they guide exhaustive testing of conditional paths. Every access control matrix, every feature flag combination, and every validation rule can be modeled as a truth table.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
A truth table lists every input combination and the result of each logical operation.
Code Example
// Generate all truth value combinations for n variables
function truthCombinations(n) {
const rows = [];
for (let i = 0; i < (1 << n); i++) {
const row = [];
for (let j = n - 1; j >= 0; j--) {
row.push(Boolean((i >> j) & 1));
}
rows.push(row);
}
return rows;
}
// Print truth table for P AND Q
console.log("P\tQ\tP AND Q");
for (const [p, q] of truthCombinations(2)) {
console.log(`${p}\t${q}\t${p && q}`);
}
console.log();
// Print truth table for P OR Q
console.log("P\tQ\tP OR Q");
for (const [p, q] of truthCombinations(2)) {
console.log(`${p}\t${q}\t${p || q}`);
}
console.log();
// Truth table for a compound expression
// (P AND Q) OR (NOT P AND NOT Q)
console.log("P\tQ\t(P AND Q) OR (NOT P AND NOT Q)");
for (const [p, q] of truthCombinations(2)) {
const result = (p && q) || (!p && !q);
console.log(`${p}\t${q}\t${result}`);
}Interactive Experiment
Try these exercises:
- Build a truth table for NOT (P AND Q). Compare it to (NOT P) OR (NOT Q). Are all rows identical?
- Generate a truth table for P OR (NOT P). What do you notice about the output column?
- Create a 3-variable truth table for (P AND Q) OR R. How many rows does it have?
- Build a truth table for the XOR operation (P and Q have different values). What expression using AND, OR, NOT produces XOR?
- Find an expression with 2 variables where the output is always false. This is called a contradiction.
Quick Quiz
Coding Challenge
Write a function called `truthTable` that takes two boolean values p and q and returns a string showing the truth table row for the expression (P AND Q) OR (NOT P). Format: 'P=true, Q=true => result'. The function should compute the result of (p AND q) OR (NOT p) for the given inputs.
Real-World Usage
Truth tables are a foundational verification tool:
- Hardware design: Logic gates are defined by truth tables. Chip designers use them to verify circuit correctness before fabrication.
- Software testing: Exhaustive condition testing follows truth table structure, testing every combination of boolean inputs.
- Access control matrices: Permission systems map (role, resource, action) combinations to allow/deny decisions, forming a truth table.
- Feature flags: With n feature flags, there are 2^n configurations. Truth tables help reason about interactions between flags.
- SQL query debugging: Complex WHERE clauses can be verified by building a truth table for the filter conditions.