Booleans: True or False
Some questions in code have only two possible answers: yes or no. Booleans help JavaScript store those answers and decide what should happen next.
What a boolean is
In programming, you often need to answer simple "yes" or "no" questions. Is the user logged in? Is the dark mode turned on? Has the payment finished?
To handle these yes/no situations, JavaScript uses a special type of data called a boolean. A boolean can only have one of two possible values: true or false.
Writing true and false in JavaScript
When you write boolean values in your code, you just type the words exactly as they are, without any quotation marks around them.
truemeans yes, on, or correct.falsemeans no, off, or incorrect.
Be careful not to use quotes! If you write "true", JavaScript sees it as plain text (a string) rather than a logical boolean value. The word true without quotes is what the computer understands as a boolean.
Using booleans in variables
You can save booleans inside variables so your program can remember them. It is a very good habit to name these variables as if you are asking a question. For example, isLightOn, hasAccess, or isComplete.
const isLightOn = true;
const hasAccess = false;
console.log(isLightOn);
console.log(hasAccess);First code example
Booleans are perfect for helping your program make decisions. We do this using an if statement. An if statement checks a boolean, and if the boolean is true, the program runs a specific block of code.
const isLoggedIn = true;
if (isLoggedIn) {
console.log("Welcome back!");
}Step-by-step explanation of the code
- First, we create a variable named
isLoggedInand set its value to the booleantrue. - Next, we write an
ifstatement. The computer checks the variable hidden inside the parentheses(isLoggedIn). - Because our
isLoggedInvariable istrue, the computer looks inside the curly braces{ }and runs the code it finds there. - Finally,
console.log("Welcome back!")prints the welcome message to your screen. IfisLoggedInwasfalse, the computer would simply skip the curly braces and print nothing.
Mini task
Go back to the code example above and change the value of isLoggedIn from true to false. Click Run and check the Output section again. What happens?
(Hint: The welcome message should disappear because the computer skips the code inside the curly braces!)
Short quiz
Question 1: Which of the following is a proper boolean value in JavaScript?
A) "false"
B) true
C) yes
Answer: B. Option A is just text because of the quotation marks, and Option C is not a recognized boolean word.
Question 2: What does an if statement do when it checks a false boolean?
A) It runs the code inside the curly braces anyway.
B) It crashes the program.
C) It skips the code inside the curly braces.
Answer: C. It skips the code entirely.
Small challenge
Now it is your turn to write your own small program!
- Create a variable called
isCompleteand set it totrue. - Write an
ifstatement that checks yourisCompletevariable. - Inside the
ifstatement's curly braces, useconsole.log()to print"Great job, you finished!". - Click Run and check the Output section to see your message.
// put your code below
console.log()Summary
- A boolean is a data type that can only be
trueorfalse. - Always write
trueandfalsewithout quotation marks. - Booleans are usually saved in variables with question-like names (like
hasAccessorisLightOn). - Programs use booleans inside
ifstatements to make choices and decide whether to run a piece of code or skip it.