JavaScript Boolean – Grasp all its concepts with a Single Guide!

The next step in our JavaScript Tutorial DataFlair Series is going to be JavaScript Boolean. Booleans are crucial during decision-making in conditional statements. We discussed booleans a little in our previous tutorial on JavaScript Data Types.

Here in this tutorial, we are going to continue with the topic in a little more depth this time, blending it with the concept of Boolean objects. And at last, we will also compare the data type boolean and the Boolean object, concluding the tutorial with Boolean methods available in JavaScript.

JavaScript Boolean Tutorial

So, without wasting any time, let’s start with the latest tutorial on JavaScript Boolean.

JavaScript Primitive boolean

One of the primitive data types that JavaScript provides us is the boolean. It understands the values true and false (JavaScript literals) that cannot be used as identifiers. The typeof operator for these values gives the result, boolean.

Use the following code in your console to see the results:

// "boolean"
typeof true
typeof false
typeof (3 > 1)
typeof (3 != 1)

Alter the values, change the case, experiment with this to master the concept.

Output:

primitive boolean

Hold On! Before moving ahead, we would strongly recommend you to go through JavaScript Objects for better understanding of further concepts.

Boolean Object

The Boolean object works as a wrapper object for the primitive Boolean. In other words, when you use the Boolean constructor with true or false as its value, you create a Boolean object. The syntax of a Boolean object is as follows:

var bool = new Boolean([value])

Here, value (optional) is the initial value of the Boolean object. The object considers all the values except ‘0’, ‘NaN’, empty string, ‘undefined’, ‘null’ as true. The Boolean object casts other data types into a Boolean object type. The length property of a Boolean object always returns 1.

The following statements create a Boolean object with an initial value of false:

// false
Boolean()
Boolean(0)
Boolean(NaN)
Boolean("")
Boolean(num) // undefined variable
Boolean(null)
Boolean(false)

Output:

boolean object

The following statements create a Boolean object with an initial value of true.

// true
Boolean(true)
Boolean("false")
Boolean(" ")
Boolean(“DataFlair”)
Boolean([]) // JavaScript array
Boolean({}) // JavaScript object
Boolean("true")

Output:

boolean object 2

Boolean vs boolean

The major difference between Boolean and boolean is clear when combined with conditional statements. A Boolean object with the initial value false evaluates to true when passed to a conditional statement. But this is not the case in boolean. The primitive booleans only return true for the reserved literal true. This is clear from the following program.

Code:

<html>
  <body>

    <script>
      var bool = false; //boolean datatype
      if(bool){
        document.write("primitive boolean returned true</br>");
      } else{
        document.write("primitive boolean returned false</br>")
      }

      var Bool = new Boolean(false); //Boolean object
      if(Bool){
        document.write("Boolean object returned true</br>");
      } else{
        document.write("Boolean object returned false</br>");
      }
    </script>

  </body>
</html>

Screenshot:

Boolean vs boolean

Output:

Boolean vs boolean output

Note: Never use a Boolean object to cast a non-boolean value to a boolean value. Keep in mind, the difference between a Boolean object and the primitive boolean, so that you don’t get confused causing unexpected results in your program.

Boolean Methods in JavaScript

Boolean methods add functionality, easing the use of booleans in our program. Here are the two most important methods discussed below. These methods can be lifesavers when you need them.

1. toString()

Depending on the value of the object, it returns either “true” or “false” as strings.

2. valueOf()

This Boolean method returns the primitive value of the Boolean object.

The following code implements both these methods for quick understanding of user. Don’t forget to run these statements in the console with me.

var bool = (8 > 2) // undefined
bool // true
typeof bool // "boolean"
result = bool.toString() // "true"
typeof result // "string"
bool.valueOf() // true
typeof bool.valueOf() // "boolean"

Output:

boolean methods

Summary

Here we conclude our tutorial on JavaScript Boolean. In this article, we took a brief look at the concept of Booleans in JavaScript. We discussed about primitive booleans and Boolean objects. Also, we learned about the relevant methods to implement JavaScript Boolean. Don’t forget to run these methods further in your console.

Next, you must go through our next blog on JavaScript Numbers

Hope you liked our article. Do share your queries and feedback in the comment section below. We will be waiting to hear from you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.