Boolean

On this page 7

A Boolean is a data type with exactly two possible values, true and false, used in programming to represent the outcome of a condition. Booleans are one of the programming fundamentals: every if statement, every loop condition and every comparison such as x > 5 produces or consumes a Boolean value, so they decide which code runs and how often.

The name honors the mathematician George Boole, whose Boolean algebra describes logic with two truth values and the operations AND, OR and NOT. Programming languages implement that algebra directly, and digital hardware implements it in logic gates, which makes the Boolean the point where software logic meets the binary nature of computers.

How Boolean values and operators work

A Boolean value usually comes from a comparison or a logical operation. Comparison operators such as equal to, not equal to, less than and greater than take two values and return true or false. Logical operators then combine Booleans into larger conditions.

The three core logical operations are AND, true only when both operands are true, OR, true when at least one operand is true, and NOT, which inverts a value. Many languages also offer XOR, exclusive or, which is true when exactly one operand is true. The truth table below shows the results for every combination of two inputs.

ABA AND BA OR BA XOR BNOT A
falsefalsefalsefalsefalsetrue
falsetruefalsetruetruetrue
truefalsefalsetruetruefalse
truetruetruetruefalsefalse
Truth table of the main Boolean operations

Most languages evaluate AND and OR with short-circuit evaluation: if the left operand already decides the result, the right operand is not evaluated. That allows guards such as user != null && user.isActive(), where the second check runs only when the first succeeds, and it also means that side effects on the right may never happen.

Booleans across programming languages

Languages differ in how strictly they separate Booleans from other types. Some have a distinct Boolean type that cannot be mixed with numbers; others treat numbers, strings and objects as true or false in a condition, a property known as truthiness.

LanguageType and literalsNotes
Cbool with true and false via stdbool.hHistorically any nonzero integer is true
C++bool with true and falseConverts implicitly to and from integers
Java and C#boolean or boolNo implicit conversion from numbers
Pythonbool with True and FalseA subclass of int; empty values are falsy
JavaScriptboolean with true and falseMany values are falsy, including 0, empty string and null
SQLBOOLEAN with TRUE, FALSE and NULLThree-valued logic with unknown
Boolean representation in common languages

Static typing helps here: in languages such as Java, a condition must be a real Boolean, so the classic mistake of writing an assignment instead of a comparison inside an if statement is rejected by the compiler rather than silently accepted.

Truthiness, falsy values and three-valued logic

In dynamically typed languages, a condition may receive a non-Boolean value and convert it automatically. The set of values treated as false, the falsy values, varies by language and is a frequent source of bugs.

ValuePythonJavaScriptRuby
Number zeroFalsyFalsyTruthy
Empty stringFalsyFalsyTruthy
Empty list or arrayFalsyTruthyTruthy
None, null or nilFalsyFalsyFalsy
The string “false”TruthyTruthyTruthy
Values treated as false in conditions

Databases add another twist. SQL uses three-valued logic, in which a comparison involving NULL yields unknown rather than true or false. A WHERE clause keeps only rows where the condition is true, so price <> 10 silently excludes rows whose price is NULL, which surprises many developers.

Using Booleans well in code

Clear Boolean code reads like a statement of fact. Naming and structure matter more than cleverness, because conditions are where logic errors hide most easily.

  • Name Boolean variables as predicates, such as isValid, hasAccess or canRetry, so that if hasAccess reads naturally.
  • Avoid redundant comparisons like if (isReady == true); write if (isReady).
  • Return the condition directly instead of using if and else to return true and false.
  • Extract complex expressions into well-named helper functions or variables.
  • Apply De Morgan’s laws: NOT (A AND B) equals NOT A OR NOT B, which helps simplify negated conditions.

A Boolean parameter in a function signature can be a design smell. A call such as render(true) hides its meaning; an enumeration or two separate functions often express the intent more clearly. Several Boolean fields that must not be true together, such as isDraft and isPublished, usually signal that a single state field would model the data better.

Storage and common mistakes

Logically, a Boolean needs one bit, but languages usually store it in one byte or more because memory is addressed in bytes. Large collections can be packed into bit sets or bit fields, which store eight flags per byte at the cost of slightly more complex access.

MistakeConsequenceCorrection
Assignment instead of comparisonCondition is always true or falseUse the equality operator; enable warnings
Relying on truthinessZero or empty string treated as missingCompare explicitly with null
Wrong operator precedenceAND binds tighter than OR unexpectedlyAdd parentheses
Ignoring NULL in SQLRows silently filtered outUse IS NULL checks or COALESCE
Negated double negativesHard-to-read conditionsRename to positive predicates
Typical Boolean mistakes and corrections

Understanding it simply

A Boolean is a yes-or-no answer that a program stores and checks. Is the user logged in? Is the cart empty? Is the password long enough? Each question has only two possible answers, true or false.

Programs make decisions by asking these questions and combining them, for example logged in AND has paid. Like a light switch that is either on or off, a Boolean has no middle position, which keeps the decision clear.

Applied in practice

A developer building an online course platform must decide who may open a lesson. The rule is that a user can watch if the lesson is free, or if the user is logged in and has an active subscription. The developer writes it as one Boolean expression: lesson.isFree || (user.isLoggedIn && user.hasActiveSubscription).

Short-circuit evaluation means that for free lessons the subscription is never checked, which saves a database lookup on the most visited pages. During testing, the team finds that guest users without an account object caused an error. Adding a null check at the start of the condition fixes it, and unit tests for each truth-table combination now confirm that every access rule behaves as intended.