Skip to content

JavaScript Boolean variable | Basics

  • by

Use var let or const keyword to create a Boolean variable 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
let exampleBoolean = true;
let anotherExample = false;

Note: Never create a boolean using the Boolean constructor function. Instead, just use the literal values, true or false.

JavaScript Boolean variable

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    var age = 0;

    // bad
    var hasAge = new Boolean(age);
    console.log(hasAge)

    // good
    var hasAge = Boolean(age);
    console.log(hasAge)

    // good
    var hasAge = !!age;
    console.log(hasAge)

    // good
    var hasAge = !age;
    console.log(hasAge)

  </script>
</body>
</html>

Output:

JavaScript Boolean variable

More Code

// falsy values: false, 0, -0, 0n, null, undefined, NaN, and the empty string ""
console.log(Boolean(false)) // false
console.log(Boolean(0)) // false
console.log(Boolean(-0)) // false
console.log(Boolean(0n)) // false
console.log(Boolean(null)) // false
console.log(Boolean(undefined)) // false
console.log(Boolean(NaN)) // false
console.log(Boolean("")) // false
console.log(typeof Boolean("")) // boolean

// truthy values: true, 1, -1, 1n, -1n, Infinity, -Infinity, " ", {}, []
console.log(Boolean(true)) // true
console.log(Boolean(1)) // true
console.log(Boolean(-1)) // true
console.log(Boolean(1n)) // true
console.log(Boolean(-1n)) // true
console.log(Boolean(Infinity)) // true
console.log(Boolean(-Infinity)) // true
console.log(Boolean(" ")) // true
console.log(Boolean({})) // true
console.log(Boolean([])) // true
console.log(typeof Boolean([])) // boolean

How to change a boolean value in JS?

Answer: To change a boolean to its opposite value you can use negation (!), for example x = !x means “set x to false if it’s truthy or to true if it’s falsy”.

<script>
        let testBool = true;
        console.log(testBool);
      
        function toggle() {
            testBool = !testBool;
            console.log(testBool);
        }
</script>

You can use Boolean variables in conditional statements, loops, and other control structures to control the flow of your program based on whether a condition is true or false. For example:

if (isActive) {
    console.log("User is active");
} else {
    console.log("User is not active");
}

if (!isLoggedOut) {
    console.log("User is logged in");
} else {
    console.log("User is logged out");
}

In JavaScript, many expressions and functions return Boolean values. For instance, comparison operators like <, >, ===, !==, <=, >=, logical operators like &&, ||, and various built-in functions can evaluate to a Boolean value.

Here’s an example using comparison operators:

var x = 5;
var y = 10;
var isGreater = x > y; // isGreater will be false because 5 is not greater than 10
console.log(isGreater); // Output will be false

And here’s an example using logical operators:

var isAdult = true;
var hasLicense = false;
var canDrive = isAdult && hasLicense; // canDrive will be false because hasLicense is false
console.log(canDrive); // Output will be false

These are just some examples of how Boolean variables and expressions are used in JavaScript to control program flow and make decisions based on conditions.

Do comment if you have any doubts or suggestions on this JS variable topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading