How To Check Boolean Value In JavaScript

Before understanding how to check Boolean value in JavaScript, let’s first understand what a Boolean value is. A Boolean value is simply a value that can either be true or false. In programming, we often need to check whether a condition is true or false and take action based on the result.

For example, we may want to check if a user is logged in before displaying certain information on a page. This can be done by storing the user’s login status in a variable and checking the variable’s value before displaying the content.

Now, let’s see how to check boolean value in JavaScript.

how to check boolean value in JavaScript

Check Boolean Value In JavaScript Using typeof

To check the Boolean value of a variable in JavaScript, we can use the typeof operator. The typeof operator returns the data type of a variable, which means it can be used to check whether a variable is a Boolean.

For example:

var loggedIn = true;

console.log(typeof loggedIn); // "boolean"

As you can see from the code above, the typeof operator returns “boolean” when used on a Boolean variable. This means we can use it to check if a variable is a Boolean:

var loggedIn = true;

if (typeof loggedIn === "boolean") {

// do something

}

else {

// do something else

}

In the code above, we first declared a variable called loggedIn and set it to true. We then used the typeof operator to check the data type of the loggedIn variable. If the loggedIn variable is a Boolean, we will execute the code inside the first block. Otherwise, we will execute the code inside the else block.

Check Boolean Value In JavaScript Using === Operator

Another way to check the Boolean value of a variable is to use the === (strict equality) operator. The === operator returns true if the two operands are equal and have the same data type.

For example:

var loggedIn = true;

console.log(loggedIn === true); // true

console.log(loggedIn === false); // false

As you can see from the code above, the === operator returns true when the two operands are equal and have the same data type. In this case, we are comparing a Boolean variable with the Boolean values true and false.

We can use the === operator to check if a variable is a Boolean:

var loggedIn = true;

if (loggedIn === true || loggedIn === false) {

// do something

}

else {

// do something else

}

In the code above, we first declared a variable called loggedIn and set it to true. We then used the === operator to check if the loggedIn variable is equal to true or false. If the loggedIn variable is a Boolean, we will execute the code inside the first block. Otherwise, we will execute the code inside the else block.

Check Boolean Value In JavaScript Using valueOf() Method

Another way to check the Boolean value of a variable is to use the valueOf() method. The valueOf() method returns the primitive value of a variable. For Boolean variables, the valueOf() method returns either true or false.

For example:

var loggedIn = true;

console.log(loggedIn.valueOf()); // true

As you can see from the code above, the valueOf() method returns true when called on a Boolean variable. This means we can use it to check if a variable is a Boolean:

var loggedIn = true;

if (loggedIn.valueOf() === true || loggedIn.valueOf === false) {

// do something

}

else {

// do something else

}

In the code above, we first declared a variable called loggedIn and set it to true. We then used the valueOf() method to check if the loggedIn variable is equal to true or false. If the loggedIn variable is a Boolean, we will execute the code inside the first block. Otherwise, we will execute the code inside the else block.

Check Boolean Value In JavaScript Using Boolean() Function

Another way to check the Boolean value of a variable is to use the Boolean() function. The Boolean() function returns a Boolean value based on the input.

For example:

var loggedIn = true;

console.log(Boolean(loggedIn)); // true

As you can see from the code above, the Boolean() function returns true when called on a Boolean variable. This means we can use it to check if a variable is a Boolean:

var loggedIn = true;

if (Boolean(loggedIn) === true || Boolean(loggedIn) === false) {

// do something

}

else {

// do something else

}

In the code above, we first declared a variable called loggedIn and set it to true. We then used the Boolean() function to check if the loggedIn variable is equal to true or false. If the loggedIn variable is a Boolean, we will execute the code inside the first block. Otherwise, we will execute the code inside the else block.

Check The String Representation Of The Variable With toString.call()

In addition to checking the boolean values using the === operator, we can also use the toString.call() method to check the string representation of the variable.

For example:

var loggedIn = true;

console.log(toString.call(loggedIn)); // "[object Boolean]"

As you can see from the code above, the toString.call() method returns ‘[object Boolean]’. This means we can use it to check if a variable is a Boolean:

var loggedIn = true;

if(loggedIn === true || loggedIn === false || toString.call(val) === "[object Boolean]"){

// do something

} else {

// do something else

}

In the code above, we first declared a variable called loggedIn and set it to true. We then used the toString.call() method to check if the loggedIn variable is equal to [object Boolean] . If the loggedIn variable is a Boolean, we will execute the code inside the first block. Otherwise, we will execute the code inside the else block.

Conclusion

In this article, we looked at many ways to check if a variable is a Boolean in JavaScript. We looked at using the typeof operator, the === operator, the valueOf() method, the Boolean function, and the toString.call() method.

All of these methods can be used to check if a variable is a Boolean in JavaScript. Thanks for reading!

Leave a Reply