CodeBlanks Logo Icon CodeBlanks

Boolean values

JavaScript course · Beginner · Lesson 88/133

Learn true and false values and expressions that return booleans.

Boolean values

In JavaScript the boolean type holds only two values: true and false. These values represent binary states such as whether a button is on or off, or whether a user is signed in.

You declare a boolean variable with let or const: const isOpen = true;. Comparison operators also return booleans: 5 > 3 gives true, 5 < 3 gives false. Such expressions are called boolean expressions.

Comparisons return booleans

Comparison operators compare two values and always return a boolean. Common operators are === (equal), !== (not equal), > (greater), < (less), >= and <=.

For example 10 === 10 gives true while "5" === 5 gives false, because === compares both value and type. The typeof operator returns the type name: typeof true is "boolean". This lets you check whether a value is a boolean.

Exercise Create boolean variables

Set isLoggedIn to false and hasPermission to true. Print both with console.log.

Show example solution
const isLoggedIn = false;
const hasPermission = true;
console.log(isLoggedIn);
console.log(hasPermission);

Exercise Write comparison expressions

Set age to 20. Set canVote to the comparison age >= 18. Set isMinor to the comparison age < 18. Print both.

Show example solution
const age = 20;
const canVote = age >= 18;
const isMinor = age < 18;
console.log(canVote);
console.log(isMinor);

Test yourself

  1. Which is a boolean value?

    • true
    • "hello"
    • 42
    • null
    Show answer

    true — Correct. true is a boolean value.

  2. Declare a boolean variable

    const isReady = ____;
    console.log(isReady);
    Show answer
    const isReady = true;
    console.log(isReady);

All lessons in Beginner