Coder Perfect

How can I tell whether a type is Boolean?

Problem

How can I tell if a variable is of the Boolean type?

There are some possibilities, for example:

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

But that does not appeal to me.

Is there a more efficient method to accomplish this?

Asked by Matias Cicero

Solution #1

That’s why there’s a typeof command. Because it is an operator, the parentheses are optional.

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

Answered by Amit Joki

Solution #2

You can simply use typeof and do something like typeof false or typeof true in pure JavaScript, and it will return “boolean”…

But it isn’t the only method to do it; I’ve created some functions below to demonstrate different ways to check for Boolean in JavaScript, as well as new frameworks. Let’s start with this one:

function isBoolean(val) {
   return val === false || val === true;
}

Alternatively, you might use the ES6 one-line method…

const isBoolean = val => 'boolean' === typeof val;

and refer to it as such!

isBoolean(false); //return true

They check it like this in Underscore source code (with the _. at the beginning of the function name):

isBoolean = function(obj) {
   return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
};

You can also check it in jQuery like this:

jQuery.type(true); //return "boolean"

If you’re using propTypes in React, you can check if a value is boolean like this:

MyComponent.propTypes = {
  children: PropTypes.bool.isRequired
};

You can also use type boolean in TypeScript:

let isDone: boolean = false;

Another option is to convert the value to boolean and see if the result is still the same, something like:

const isBoolean = val => !!val === val;

or like:

const isBoolean = val => Boolean(val) === val;

and call it!

isBoolean(false); //return true

It’s not a good idea to use a framework for this because it’s just a simple JavaScript check.

Answered by Alireza

Solution #3

If all you want to do is look for a primitive value,

typeof variable === 'boolean'

If you have booleans generated with the constructor for whatever unexplained reason, they’re not truly booleans, but objects carrying a primitive boolean value, and one way to check for both primitive booleans and objects made with new Boolean is to:

function checkBool(bool) {
    return typeof bool === 'boolean' || 
           (typeof bool === 'object' && 
            bool !== null            &&
           typeof bool.valueOf() === 'boolean');
}

Answered by adeneo

Solution #4

With or without jQuery, there are three “basic” ways to check this.

The third will only return true if you create a new Boolean class and pass that in.

To expand on primitives coercion (as mentioned in #1), all primitives types can be tested in the following manner:

Answered by iSkore

Solution #5

To accomplish this, you can use pure Javascript:

var test = true;
if (typeof test === 'boolean')
   console.log('test is a boolean!');

Answered by Morry

Post is based on https://stackoverflow.com/questions/28814585/how-to-check-if-type-is-boolean