Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if type is Boolean

How can I check if a variable's type is of type Boolean?

I mean, there are some alternatives such as:

if(jQuery.type(new Boolean()) === jQuery.type(variable))       //Do something.. 

But that doesn't seem pretty to me.

Is there a cleaner way to achieve this?

like image 817
Matias Cicero Avatar asked Mar 02 '15 16:03

Matias Cicero


People also ask

What data type is boolean?

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra.

How do you check if a boolean is true in Python?

Syntax. If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. If boolean expression evaluates to FALSE, then the first set of code after the end of the if statement(s) is executed.


1 Answers

That's what typeof is there for. The parentheses are optional since it is an operator.

if (typeof variable == "boolean") {     // variable is a boolean } 
like image 65
Amit Joki Avatar answered Oct 25 '22 20:10

Amit Joki