Last updated: Mar 3, 2024
Reading timeยท4 min
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.
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 typeof operator returns a string that indicates the type of a value.
Here are some examples:
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"
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.
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'); }
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.
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.
This is a three-step process:
false
.true
.const bool = true; if (bool === true || bool === false) { // ๐๏ธ this runs console.log('โ type is boolean'); } else { console.log('โ๏ธ type is NOT boolean'); }
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
.
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.
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 function takes a value as a parameter and returns true
if the value is a
boolean and false
otherwise.
toString.call()
You can also use the toString.call()
method to check if a value is a boolean.
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'); }
Here is an example of defining a reusable function.
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.
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.
if (typeof value === 'boolean') { console.log('โ type is boolean'); } else { // ๐๏ธ this runs console.log('โ๏ธ type is NOT boolean'); }
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.
lodash
First, make sure you have lodash
installed by running the following command
from your terminal.
# ๐๏ธ 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.
import _ from 'lodash'; console.log(_.isBoolean(true)); // ๐๏ธ true console.log(_.isBoolean(false)); // ๐๏ธ true console.log(_.isBoolean('hello')); // ๐๏ธ false console.log(_.isBoolean(null)); // ๐๏ธ false
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.
instanceof
operator to check if a value is a booleanYou should never use the instanceof
operator to check if a value is of type
boolean.
// โ๏ธ 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 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.
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'); }