EasyCode365EasyCode365

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.

  • true means yes, on, or correct.
  • false means 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.

Can edit
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.

Can edit
const isLoggedIn = true;

if (isLoggedIn) {
  console.log("Welcome back!");
}

Step-by-step explanation of the code

  • First, we create a variable named isLoggedIn and set its value to the boolean true.
  • Next, we write an if statement. The computer checks the variable hidden inside the parentheses (isLoggedIn).
  • Because our isLoggedIn variable is true, 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. If isLoggedIn was false, 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!

  1. Create a variable called isComplete and set it to true.
  2. Write an if statement that checks your isComplete variable.
  3. Inside the if statement's curly braces, use console.log() to print "Great job, you finished!".
  4. Click Run and check the Output section to see your message.
Can edit
// put your code below

console.log()

Summary

  • A boolean is a data type that can only be true or false.
  • Always write true and false without quotation marks.
  • Booleans are usually saved in variables with question-like names (like hasAccess or isLightOn).
  • Programs use booleans inside if statements to make choices and decide whether to run a piece of code or skip it.