How to Check if a Variable is true or false in JavaScript
In JavaScript, a common task is to check if a variable holds the specific boolean value true or false. This is a more precise check than simply testing if a value is "truthy" or "falsy," and it's essential for writing clear and bug-free conditional logic.
This guide will explain the crucial difference between checking for true and checking for "truthiness." You will learn how to use the strict equality (===) operator to correctly and safely test for the exact boolean values true and false.
CRITICAL: true vs. "Truthy"
This is the most important concept to understand, and a common source of bugs for developers.
- The Boolean
true: This is one of JavaScript's primitive types. There is only onetruevalue. - "Truthy" Values: This is a much broader category. A value is considered "truthy" if it coerces to
truein a boolean context (like anifstatement). Almost every value in JavaScript is truthy.
The only values that are not truthy are the six falsy values: false, 0, "", null, undefined, and NaN.
The Problem with a Simple if Statement
A simple if (myVar) check only tests for truthiness, not for a specific value of true.
const myVar = 'hello'; // This is a truthy value, but it is NOT `true`
if (myVar) {
// This block runs because 'hello' is truthy...
console.log('myVar is truthy.');
}
if (myVar === true) {
// ...but this block does NOT run, because 'hello' is not strictly equal to `true`.
console.log('myVar is equal to true.');
}
If you need to know if a variable is exactly the boolean true, you must use a more precise comparison.
The Core Method (Recommended): The Strict Equality Operator (===)
The strict equality operator (===) is the definitive tool for this job. It checks if two values are equal without performing any type coercion. This means it will only return true if the values are of the same type and have the same value.
Example:
console.log(true === true); // Output: true
console.log(false === false); // Output: true
// It correctly distinguishes `true` from truthy values.
console.log(true === 1); // Output: false
console.log(true === 'true'); // Output: false
console.log(true === []); // Output: false
For this reason, === is the safest and most recommended way to check for a specific boolean value.
How to Check if a Variable is true
Use the strict equality operator to compare the variable directly to true.
const userIsActive = true;
const userIsAdmin = false;
if (userIsActive === true) {
console.log('The user is active.');
} else {
console.log('The user is not active.');
}
Output:
The user is active.
How to Check if a Variable is false
Similarly, use the strict equality operator to compare the variable directly to false.
const userIsActive = true;
const userIsAdmin = false;
if (userIsAdmin === false) {
console.log('The user is not an admin.');
} else {
console.log('The user is an admin.');
}
Output:
The user is not an admin.
Conclusion
While a simple if (myVar) is great for checking if a value is "truthy," it is not the correct tool for checking if a variable is specifically true or false.
The key takeaways are:
- Understand the critical difference between a boolean value (
true) and a "truthy" value (any value that is not one of the six falsy values). - To check if a variable is exactly
trueorfalse, always use the strict equality operator (===). - The correct syntax is
if (myVar === true)orif (myVar === false).
This explicit and strict comparison makes your code more readable, predictable, and less prone to bugs caused by unintended type coercion.