How to check if a variable is boolean in JavaScript?


In this tutorial, we will learn to check if the variable is a Boolean in JavaScript. In JavaScript, if we match the two different variables with the same value and different data types using the equality operator, it returns a true value. It means if we compare a twovariable with the true value but different data types such as Boolean and string, it will generate a positive result.

To overcome these problems, we need to check if the variable is of type Boolean or not before matching it with other values.

Users can use the three methods below to check the variable type.

  • Using the typeof Operator

  • Using the strict equality operator (===)

  • Using the tostring.call() Method

Using the typeof Operator

The typeof operator is used to check the variable type in JavaScript. It returns the type of variable. We will compare the returned value with the “boolean” string, and if it matches, we can say that the variable type is Boolean.

Users can use the below syntax to use the typeof operator to check if the variable is of Boolean type or not.

Syntax

typeof variable

Parameters

  • variable − It can be any type of variable.

Example

In the below example, we have checked the type of the different variables. Users can see the result in the output.

<html> <head> <title>Check if variable is of boolean type</title> </head> <body> <h2>Check if variable is of boolean type in JavaScript using <i> typeof </i> operator.</h2> <h4>output for value true</h4> <div id = "result1"></div> <h4>Output for "true"</h4> <div id = "result2"></div> <script> let result1 = document.getElementById("result1"); let result2 = document.getElementById("result2"); let bool = true; result1.innerHTML = typeof bool; bool = "true"; result2.innerHTML = typeof bool; </script> </body> </html>

In the above output, users can see that “true” value, it returns the type string, and for true value, typeof operator returns Boolean value.

Using the strict equality operator (===)

When we use the strict equality operator to compare the two variables, it compares both variables' values and data types in JavaScript. As users know Boolean data type has two values which are true and false, and we will compare the variable with both Boolean values. If one of the match conditions returns true, the variable is of Boolean type.

Syntax

Follow the below syntax to check for Boolean type variable using strict equality operator.

If( variable === true || variable === false ) {
   
   // variable is of Boolean type.
}

Example

In the below example, we have compared the bool name variable with the true and false Boolean values. Users can see the result in the output.

<html> <head> <title>Check if variable is of boolean type</title> </head> <body> <h2>Check if variable is of boolean type in JavaScript using <i> strict equality </i> operator.</h2> <div id = "result1"></div> <script> let result1 = document.getElementById("result1"); let bool = true; if (bool === true || bool === false) { result1.innerHTML = "variable bool is type of boolean."; } else { result1.innerHTML = "variable bool is not type of boolean."; } </script> </body> </html>

Using the tostring.call() Method

In this section, we will learn to use the tostring.call() method to check if the variable is a type of boolean or not. It works similarly to the typeof operator but returns a different string, rather than only the variable's data type. This method returns the string similar to ‘[object data_type]’.

Syntax

Users can follow the below syntax to use the tostring.call() method.

let result = toString.call( variable ) === '[object Boolean]'

Parameters

  • variable − It is a variable for which we need to check data type.

Example

In the below example, we have used the tostring.call() method to check the variable type and compares it with the ‘[ object boolean ]’ string. If both string matches, variable is of boolean type.

<html> <head> <title>Check if variable is of boolean type</title> </head> <body> <h2>Check if variable is of boolean type in JavaScript using <i> tostring.call() </i> method.</h2> <div id = "result1"></div> <script> let result1 = document.getElementById("result1"); let bool = true; if ( toString.call(bool) === '[object Boolean]' ) { result1.innerHTML = "variable bool is type of boolean."; } else { result1.innerHTML = "variable bool is not type of boolean. "; } </script> </body> </html>

We have successfully learned to check if the variable is of Boolean type or not in this tutorial. In the first approach, we have used the typeof operator, which can also be used to check the data type of any variable.

In the third approach, we have used the tostring.call() method, which works the same as the type of operator. However, a strict equality operator is the best solution rather than calling the method.

Updated on: 08-Aug-2022

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements