What is a boolean?
In short
A boolean is a value that can only be true or false. Programs use booleans to decide what to do.
Every question a program asks has a yes-or-no answer underneath: is the password right, is the basket empty, is the player out of lives? A boolean is that answer. Comparing two values, like age >= 18, gives you a boolean.
You can combine booleans with and, or and not to ask bigger questions, such as "is it the weekend and is the shop open?"
Python
Comparisons give you True or False
age = 16
print(age >= 18)
print(age >= 13 and age < 18)Output
False
TrueJavaScript
The same idea in JavaScript
const age = 16;
console.log(age >= 18);
console.log(age >= 13 && age < 18);Output
false
trueLearn it properly
These free lessons teach it step by step, with questions and a program you write yourself.
- Booleans and ComparisonsLearn Python →
- Booleans and Logical OperatorsLearn JavaScript →
- Comparisons and BooleansLearn C →
Try your first lesson free
Codiqo teaches coding in five-minute lessons with real code. Free on the web, iPhone and Android.