How to Check if a Value is a Boolean in JavaScript
Validating data types is a fundamental aspect of writing robust JavaScript code. A common requirement is to check if a variable holds a boolean value (true or false) before using it in conditional logic. The standard, most reliable, and direct way to perform this check is with the typeof operator.
This guide will teach you how to use the typeof operator to reliably check for booleans and explain why it is superior to other, less common methods.
The Core Method (Recommended): The typeof Operator
The typeof operator is the definitive tool for this job. It returns a string indicating the type of the unevaluated operand. When used on a boolean value, it returns the string "boolean".
For example, you have a variable and you want to confirm that it is a true boolean before using it.
// Problem: How to safely check if `isActive` is a boolean?
const isActive = true;
const username = 'Alice';
Solution:
const isActive = true;
// Check if the type is exactly 'boolean'
if (typeof isActive === 'boolean') {
console.log('The value is a boolean.');
} else {
console.log('The value is NOT a boolean.');
}
Output:
The value is a boolean.
This is the recommended best practice because it is a direct, readable, and highly performant check of the value's primitive type.
How typeof Handles Non-Boolean Values
The typeof operator correctly distinguishes booleans from other data types.
For example:
function isBoolean(value) {
return typeof value === 'boolean';
}
// Example Usage:
console.log(isBoolean(true)); // Output: true
console.log(isBoolean(false)); // Output: true
console.log(isBoolean('true')); // Output: false (this is a string)
console.log(isBoolean(1)); // Output: false (this is a number)
console.log(isBoolean(null)); // Output: false
console.log(isBoolean(undefined)); // Output: false
Alternative (but Less Common) Methods
While typeof is the best tool, you may encounter other methods in older codebases.
- Direct Comparison:
if (value === true || value === false)- This works, but it is more verbose than
typeof value === 'boolean'.
- This works, but it is more verbose than
instanceof Boolean:if (value instanceof Boolean)- This is not a reliable check and should be avoided. It only returns
truefor boolean objects created withnew Boolean(), not for the primitive valuestrueandfalse. This is almost never what you want.
- This is not a reliable check and should be avoided. It only returns
Why typeof is the Best Practice
typeof is the superior method for checking for booleans for a few key reasons:
- Clarity and Intent:
typeof value === 'boolean'is a self-documenting piece of code. Its purpose is immediately clear. - Safety with Undeclared Variables:
typeofis one of the few operators that is safe to use on a variable that has not been declared. It will return"undefined"instead of throwing aReferenceError. - Correctly Handles Primitives: Unlike
instanceof, it correctly identifies the primitive boolean valuestrueandfalse.
Conclusion
For checking if a value is a boolean in JavaScript, the choice is clear and simple.
- The
typeof value === 'boolean'check is the recommended best practice. It is the safest, most reliable, and most idiomatic way to verify that a variable holds a boolean before you use it. - Avoid other methods like
instanceof Booleanor verbose||checks, astypeofprovides a superior, single solution.