Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if a variable is boolean in JavaScript?
In JavaScript, determining if a variable is a boolean can be tricky because of type coercion. When using the equality operator (==), JavaScript converts values, so true == "true" returns true even though they're different types. To accurately check boolean types, we need specific methods.
Here are three reliable approaches to check if a variable is a boolean:
Using the typeof operator
Using the strict equality operator (===)
Using Object.prototype.toString.call()
Using the typeof Operator
The typeof operator returns a string indicating the type of a variable. For booleans, it returns "boolean".
Syntax
typeof variable === "boolean"
Example
<html>
<head>
<title>Check if variable is boolean</title>
</head>
<body>
<h2>Checking boolean type using typeof operator</h2>
<div id="result1"></div>
<div id="result2"></div>
<script>
let boolValue = true;
let stringValue = "true";
document.getElementById("result1").innerHTML =
"typeof true: " + typeof boolValue;
document.getElementById("result2").innerHTML =
'typeof "true": ' + typeof stringValue;
</script>
</body>
</html>
typeof true: boolean typeof "true": string
Using the Strict Equality Operator (===)
Since booleans only have two values (true and false), we can check if a variable matches either value using strict equality.
Syntax
if (variable === true || variable === false) {
// variable is boolean
}
Example
<html>
<head>
<title>Check boolean with strict equality</title>
</head>
<body>
<h2>Checking boolean type using strict equality</h2>
<div id="result"></div>
<script>
function checkBoolean(value) {
return value === true || value === false;
}
let tests = [true, false, "true", 1, 0, null];
let output = "";
tests.forEach(test => {
output += `${JSON.stringify(test)}: ${checkBoolean(test)}<br>`;
});
document.getElementById("result").innerHTML = output;
</script>
</body>
</html>
true: true false: true "true": false 1: false 0: false null: false
Using Object.prototype.toString.call()
This method provides the most precise type checking by returning the internal [[Class]] property of an object.
Syntax
Object.prototype.toString.call(variable) === '[object Boolean]'
Example
<html>
<head>
<title>Check boolean with toString.call()</title>
</head>
<body>
<h2>Checking boolean type using Object.prototype.toString.call()</h2>
<div id="result"></div>
<script>
function isBooleanObject(value) {
return Object.prototype.toString.call(value) === '[object Boolean]';
}
let primitiveBoolean = true;
let booleanObject = new Boolean(false);
let string = "true";
let output = `Primitive boolean: ${isBooleanObject(primitiveBoolean)}<br>`;
output += `Boolean object: ${isBooleanObject(booleanObject)}<br>`;
output += `String: ${isBooleanObject(string)}`;
document.getElementById("result").innerHTML = output;
</script>
</body>
</html>
Primitive boolean: true Boolean object: true String: false
Comparison
| Method | Speed | Detects Boolean Objects | Simplicity |
|---|---|---|---|
typeof |
Fast | Yes | High |
| Strict Equality | Fast | No | Medium |
toString.call() |
Slower | Yes | Low |
Conclusion
For most cases, typeof variable === "boolean" is the recommended approach due to its simplicity and performance. Use strict equality when you specifically need to check only primitive boolean values, and Object.prototype.toString.call() for the most comprehensive type checking.
