How to check if type is Boolean using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Check if a value's type is Boolean using JavaScript

Use the typeof operator to check if a value is of boolean type.

The typeof operator will return "boolean" if the type of the value is boolean.

index.js
const bool = true; if (typeof bool === 'boolean') { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… the value is a boolean'); } else { console.log('โ›”๏ธ the value is NOT a boolean'); }

check if value is of type boolean

The code for this article is available on GitHub

The typeof operator returns a string that indicates the type of a value.

Here are some examples:

index.js
console.log(typeof true); // ๐Ÿ‘‰๏ธ "boolean" console.log(typeof false); // ๐Ÿ‘‰๏ธ "boolean" console.log(typeof function () {}); // ๐Ÿ‘‰๏ธ "function" console.log(typeof null); // ๐Ÿ‘‰๏ธ "object" console.log(typeof []); // ๐Ÿ‘‰๏ธ "object" console.log(typeof {}); // ๐Ÿ‘‰๏ธ "object" console.log(typeof ''); // ๐Ÿ‘‰๏ธ "string" console.log(typeof 0); // ๐Ÿ‘‰๏ธ "number"

using typeof operator in javascript

When used with a value of true or false, the typeof operator returns the string "boolean" and that's exactly what we check for in the if statement.

index.js
const bool = true; if (typeof bool === 'boolean') { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… the value is a boolean'); } else { console.log('โ›”๏ธ the value is NOT a boolean'); }
The code for this article is available on GitHub

If the condition is met, the if block runs, otherwise, the else block runs.

If you have to check if a value is of type boolean often, define a reusable function.

index.js
function isBoolean(value) { return typeof value === 'boolean'; } console.log(isBoolean(true)); // ๐Ÿ‘‰๏ธ true console.log(isBoolean(false)); // ๐Ÿ‘‰๏ธ true console.log(isBoolean(undefined)); // ๐Ÿ‘‰๏ธ false console.log(isBoolean('')); // ๐Ÿ‘‰๏ธ false console.log(isBoolean(null)); // ๐Ÿ‘‰๏ธ false

The isBoolean function takes a value as a parameter and returns true if the value is of type boolean and false otherwise.

An alternative approach is to use the logical OR (||) operator.

# Check if a value is of type boolean using logical OR (||) operator

This is a three-step process:

  1. Check if the value is equal to false.
  2. Check if the value is equal to true.
  3. If either condition is met, the value is a boolean.
index.js
const bool = true; if (bool === true || bool === false) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… type is boolean'); } else { console.log('โ›”๏ธ type is NOT boolean'); }

check if value is of type boolean using logical or operator

The code for this article is available on GitHub

We used the logical or (||) operator to chain 2 conditions.

If either condition evaluates to true, the if block runs.

Our conditions check if the value is equal to true or equal to false.

Boolean values can only be true and false, so if either condition is met, the value has a type of boolean.

If you have to check if a value is a boolean often, define a reusable function.

index.js
function isBoolean(value) { return value === true || value === false; } console.log(isBoolean(true)); // ๐Ÿ‘‰๏ธ true console.log(isBoolean(false)); // ๐Ÿ‘‰๏ธ true console.log(isBoolean(undefined)); // ๐Ÿ‘‰๏ธ false console.log(isBoolean('')); // ๐Ÿ‘‰๏ธ false console.log(isBoolean(null)); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

The function takes a value as a parameter and returns true if the value is a boolean and false otherwise.

# Check if a value is of type boolean using toString.call()

You can also use the toString.call() method to check if a value is a boolean.

index.js
const value = true; if ( Object.prototype.toString.call(value) === '[object Boolean]' ) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… type is boolean'); } else { console.log('โ›”๏ธ type is NOT boolean'); }

check if value is of type boolean using tostring call

The code for this article is available on GitHub

Here is an example of defining a reusable function.

index.js
function isBoolean(value) { return Object.prototype.toString.call(value) === '[object Boolean]'; } console.log(isBoolean(true)); // ๐Ÿ‘‰๏ธ true console.log(isBoolean(false)); // ๐Ÿ‘‰๏ธ true console.log(isBoolean(undefined)); // ๐Ÿ‘‰๏ธ false console.log(isBoolean('')); // ๐Ÿ‘‰๏ธ false console.log(isBoolean(null)); // ๐Ÿ‘‰๏ธ false

The Object.prototype.toString.call() method can be used to stringify a value.

When a boolean value is stringified, the string '[object Boolean]' is returned.

You shouldn't have to use this approach as it relies on the language's internals and is unintuitive.

Which approach you pick is a matter of personal preference. I'd use the typeof operator as I find it quite direct and intuitive.

One thing to note about the typeof operator is that it doesn't throw an error even if the variable has not been declared.

index.js
if (typeof value === 'boolean') { console.log('โœ… type is boolean'); } else { // ๐Ÿ‘‡๏ธ this runs console.log('โ›”๏ธ type is NOT boolean'); }
The code for this article is available on GitHub

A variable with the name value has not been declared in the code sample, but the code didn't throw an error.

The typeof operator returns undefined for variables that have not been declared and doesn't throw an error.

If you use the lodash library, you can also check if a value is a boolean using the isBoolean() method.

# Check if a value is of type boolean using lodash

First, make sure you have lodash installed by running the following command from your terminal.

shell
# ๐Ÿ‘‡๏ธ initialize package.json if you don't have one npm init -y npm install lodash

Now you can import and use the isBoolean() method to check if a value is a boolean.

index.js
import _ from 'lodash'; console.log(_.isBoolean(true)); // ๐Ÿ‘‰๏ธ true console.log(_.isBoolean(false)); // ๐Ÿ‘‰๏ธ true console.log(_.isBoolean('hello')); // ๐Ÿ‘‰๏ธ false console.log(_.isBoolean(null)); // ๐Ÿ‘‰๏ธ false

check if value is of type boolean using lodash

The code for this article is available on GitHub

The isBoolean function returns true if the supplied value is a boolean and false otherwise.

Even if you already use lodash in your project, I would advise against using the isBoolean method as the typeof operator is sufficient and doesn't require you to use an external library.

# Don't use the instanceof operator to check if a value is a boolean

You should never use the instanceof operator to check if a value is of type boolean.

index.js
// โ›”๏ธ BAD - don't do this function isBoolean(value) { return value instanceof Boolean; } console.log(isBoolean(true)); // ๐Ÿ‘‰๏ธ false console.log(isBoolean(false)); // ๐Ÿ‘‰๏ธ false console.log(isBoolean('hello')); // ๐Ÿ‘‰๏ธ false console.log(isBoolean(new Boolean(true))); // ๐Ÿ‘‰๏ธ true console.log(isBoolean(new Boolean(false))); // ๐Ÿ‘‰๏ธ true
The code for this article is available on GitHub

The syntax for the instanceof operator is object instanceof constructor.

The operator returns true if the prototype property of the constructor appears anywhere in the prototype chain of the object.

Notice that the operator returns true only for object-wrapped booleans.

These are booleans that were created with the new Boolean() constructor.

You should never use the new Boolean() constructor in your code, so you should never have to use the instanceof operator to check if a value is a boolean.

The typeof operator is all you need in order to check if a value stores a boolean in JavaScript.

index.js
const bool = true; if (typeof bool === 'boolean') { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… the value is a boolean'); } else { console.log('โ›”๏ธ the value is NOT a boolean'); }

using typeof operator to check for boolean

The code for this article is available on GitHub
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev