How to check if type is Boolean

Matias Cicero picture Matias Cicero · Mar 2, 2015 · Viewed 256.2k times · Source

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?

Answer

Amit Joki picture Amit Joki · Mar 2, 2015

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

if (typeof variable === "boolean"){
  // variable is a boolean
}