JavaScript booleans

web
mobile

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
  • TRUE / FALSE

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
                

Rate this lesson

Previous lesson Next lesson

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

About the author

User avatar
Michal Szymanski
Co-Founder at MDBootstrap & BrandFlow. Entrepreneur, web developer, UI/UX designer, marketing analyst. Dancer and nerd in one.