JavaScript booleans

Author: MDBootstrap

A JavaScript Boolean represents one of two values: true or false.

Boolean Values

Very often, in programming, you will need a data type that can only have one of two values, like

  • YES / NO
  • ON / OFF

For this, JavaScript has a Boolean data type. It can only take the values true or false.

The Boolean() Function

You can use the Boolean() function to find out if an expression (or a variable) is true:

                      Boolean(10 > 9)        // returns true

Or even easier:

                      (10 > 9)              // also returns true
                      10 > 9                // also returns true

Everything With a "Value" is True

                      var b1 = Boolean(100);            // Returns true
                      var b2 = Boolean(3.14);           // Returns true
                      var b3 = Boolean(-15);            // Returns true
                      var b4 = Boolean("Hello");        // Returns true
                      var b5 = Boolean('false');        // Returns true
                      var b6 = Boolean(1 + 7 + 3.14);   // Returns true

Everything Without a "Value" is False

                      var b7 = Boolean(0);      // Returns false
                      var b8 = Boolean(-0);     // Returns false
                      var b9 = Boolean("");     // Returns false
                      var b10 = Boolean();       // Returns false
                      var b11 = Boolean(null);   // Returns false

Previous lesson Next lesson

Spread the word:
Do you need help? Use our support forum

About the author