A boolean is a data type that can hold only two possible values: true or false. It is named after the mathematician George Boole, and it is the simplest way a program represents a yes-or-no answer. Whenever your code asks a question, like is the user logged in or is this number greater than ten, the answer comes back as a boolean. Those two values are the foundation of every decision a program makes, which is why booleans show up everywhere even though they only ever say true or false.
How a boolean works
A boolean lives in a single variable that is either true or false. You can set it directly, or, more often, it is the result of a comparison or test. When you write a condition like age >= 18, the program evaluates it and hands back true or false. That result then steers what happens next.
This is why booleans are tied so tightly to control flow. An if-statement runs its block only when its condition is true. A while-loop keeps going as long as its condition stays true. So a boolean is less about storing data and more about gating behavior.
let isLoggedIn = true; // a boolean set directly
let canVote = age >= 18; // a boolean from a comparison
if (isLoggedIn) {
// this runs only when isLoggedIn is true
showDashboard();
}
Operators that produce and combine booleans
Two groups of operators matter. Comparison operators produce a boolean, and logical operators combine booleans.
| Operator |
Type |
Example |
Result |
== or === |
Comparison |
5 === 5 |
true |
> < >= <= |
Comparison |
3 > 7 |
false |
&& (and) |
Logical |
true && false |
false |
|| (or) |
Logical |
true || false |
true |
! (not) |
Logical |
!true |
false |
The logical operators are how you express compound conditions: allow access if the user is an admin AND the account is active, for example.
Truthy, falsy, and the common confusion
Here is where beginners get tripped up. Many languages, including JavaScript and Python, let you use non-boolean values where a boolean is expected. They convert the value to true or false on the fly. These are called truthy and falsy values.
For example, an empty string, the number zero, and null are usually treated as false, while a non-empty string or any non-zero number is treated as true. This is convenient, but it is not the same as the value actually being a boolean. A variable can be truthy without being the literal value true. Knowing the difference saves hours of debugging.
If you are still learning the wider vocabulary, the companion guide on what a data type is puts booleans in context alongside numbers and strings.
Common mistakes
- Writing
if (x == true). If x is already a boolean, just write if (x).
- Confusing truthy with true. Convenient coercion is not the same as a real boolean value.
- Using
= instead of ==. A single equals assigns; you need a comparison operator to get a boolean back.
- Over-nesting conditions. Combine booleans with
&& and || instead of stacking if-statements.
FAQ
What are the only two values a boolean can have?
True and false. That is the entire range, which is what makes booleans ideal for decisions.
Why is it called a boolean?
It is named after George Boole, whose 19th-century work on logic underlies the true-or-false algebra computers use.
Is zero a boolean?
No, zero is a number. But in many languages zero is falsy, meaning it behaves like false when used in a condition.
What is the difference between truthy and true?
True is the literal boolean value. Truthy means a non-boolean value that a language treats as true in a condition, like a non-empty string.
Where to go next
What is a data type, What is a string in programming, and What is a loop in programming.