Skip to main content

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 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.
note

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'.
  • instanceof Boolean: if (value instanceof Boolean)
    • This is not a reliable check and should be avoided. It only returns true for boolean objects created with new Boolean(), not for the primitive values true and false. This is almost never what you want.

Why typeof is the Best Practice

typeof is the superior method for checking for booleans for a few key reasons:

  1. Clarity and Intent: typeof value === 'boolean' is a self-documenting piece of code. Its purpose is immediately clear.
  2. Safety with Undeclared Variables: typeof is 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 a ReferenceError.
  3. Correctly Handles Primitives: Unlike instanceof, it correctly identifies the primitive boolean values true and false.

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 Boolean or verbose || checks, as typeof provides a superior, single solution.