3) Learn to Crawl Before You Can Run Lesson

What is a Boolean in JavaScript

3 min to complete · By Ian Currie

In this lesson you will learn about booleans. Perhaps the simplest of all Javascript data types: they are either true or false and nothing in between.

What Values Can Booleans Have

Booleans are similar to a light switch, they can be either on (true) or off (false). They are what is known as a binary value, just like what your CPU runs on, 1s and 0s.

When to Use Booleans

You will use booleans to create conditional statements. This allows you to define separate avenues of instruction depending on the value of a Boolean. You will examine this concept in a different section of the course. For now, you will look at the details of the values themselves.

Boolean Syntax

Booleans are written in lowercase and do not have quotation marks around them. Putting quotation marks around them would turn them into a string which is a different data type.

let delivered = "true";
let running = true;

In the example above, only the variable running has a boolean as its value. The variable delivered has a value of a string.

let amILearning = true;
const willIFail = false;

if (amILearning == false) {
    takeABreak();
}
Illustration of a lighthouse

Above are two different ways you could declare a boolean variable. You declare the willIFail with a constant value of false, for obvious reasons! Below it is an example of a conditional statement that checks the amILearning variable, if it is false, it calls the function called takeABreak - handy to have when you are going through a course full of new information! You will go over conditional statements and functions in depth later.

Summary: What Is a Boolean

  • Booleans can only have a value of true or false, and nothing in between
  • They are often used in conditional statements