How to Write Boolean Variables in JavaScript

Hey - Nick here! This page is a free excerpt from my $99 course JavaScript Fundamentals.

If you want the full course, click here to sign up and create an account.

I have a 30-day satisfaction guarantee, so there's no risk (and a ton of upside!) in signing up for this course and leveling up your JavaScript developer skills today!

Along with strings and numbers, booleans are one of the most important data types in JavaScript.

This lesson will teach you how to write boolean variables in JavaScript.

What is a Boolean Value in JavaScript

A boolean variable is used to identify whether a condition is true or false.

Accordingly, boolean values can only assume two values:

  1. true
  2. false

Here is an example of how to create a boolean variable with a value of true:

let exampleBoolean = true;

Similarly, here is an example of how to create a boolean variable with a value of false:

let anotherExample = false;

Notice how I used the let keyword when creating these two variables, and not my normal default const keyword.

This is because JavaScript boolean variables are generally intended to be modified over the course of a program.

Said differently, boolean variables are designed to be toggled to change the functionality of your applications.

We will learn how to do this in our next lesson, when we learn about how to write if statements in JavaScript.

For now, let's move on to learning how to calculate boolean values in JavaScript instead of manually assigning them with the true or false keywords.

How to Calculate Boolean Values in JavaScript

It is possible to calculate Boolean values in JavaScript using comparison operators, which output true or false depending on whether some mathematical condition is met.

Here are the major comparison operators in JavaScript:

  • <: less than
  • >: greater than
  • <=: less than or equal to
  • >=: greater than or equal to

Here are a few examples of how you can use comparison operators to generate true or false values in JavaScript:

5 < 6;

//Returns true

5 > 6;

//Returns false

5 <= 6;

//Returns true

5 >= 5;

//Returns true

We can also test equality in JavaScript. There are two ways to do this: using the == operator and using the === operator. Here are two examples:

const value = 30;

value === 30;

//Returns true

value == 30;

//Returns true

value === 40;

//Returns false

value == 60;

//Returns false

You are probably wondering what the difference is between the == and === equality operators.

The === operator checks for equality between both value and type. The == operator only checks for the equality of value.

Here is an example so you can see this concept in action:

10 == '10';

//Returns true, since their value is the same but their type is not

10 === '10'

//Returns false, since their value is the same but their type is not

In practice, you should almost always use the === operator when calculating equality in JavaScript applications.

Final Thoughts

In this lesson, you learned how to write boolean values in JavaScript.

Here is a summary of the topics we covered:

  • The two values that can be assigned to a boolean variable
  • Comparison operators in JavaScript
  • The two JavaScript operators available for testing equality