Booleans
Booleans are one of the simplest and most important data types in JavaScript.
A boolean can have only two possible values:
true;
false;You can think of a boolean like a yes/no answer:
- Is the user logged in?
trueorfalse - Is the form valid?
trueorfalse - Is the cart empty?
trueorfalse
Booleans are the foundation of decisions in programming.
Creating Booleans
Create booleans by writing true or false without quotes.
const isJavaScriptFun = true;
const isSkyGreen = false;
console.log(typeof isJavaScriptFun); // boolean
console.log(typeof isSkyGreen); // booleanDo not put quotes around boolean values.
const isActive = "true";
console.log(typeof isActive); // string"true" is a string, not a boolean. This difference matters when you write conditions.
Booleans from Comparisons
You will often get booleans from comparisons.
const age = 20;
console.log(age > 18); // true
console.log(age === 25); // false
console.log(age < 0); // falseComparison operators return true or false.
Common comparison operators:
| Operator | Meaning | Example |
|---|---|---|
> |
Greater than | 10 > 5 gives true |
< |
Less than | 10 < 5 gives false |
>= |
Greater than or equal to | 10 >= 10 gives true |
<= |
Less than or equal to | 8 <= 10 gives true |
=== |
Strictly equal | 5 === 5 gives true |
!== |
Strictly not equal | 5 !== 3 gives true |
Use === and !== for equality checks. They are safer than == and != because they avoid unexpected type conversion.
console.log(5 === "5"); // false
console.log(5 == "5"); // trueThe strict comparison is usually what you want.
Booleans in if Statements
Booleans control which code runs.
const isLoggedIn = true;
if (isLoggedIn) {
console.log("Welcome back!");
} else {
console.log("Please log in.");
}This condition:
if (isLoggedIn) {means:
if (isLoggedIn === true) {The shorter version is common and readable.
Logical Operators
JavaScript has logical operators for combining or reversing boolean values.
&& means AND
Both sides must be true.
const isLoggedIn = true;
const hasPermission = true;
console.log(isLoggedIn && hasPermission); // trueIf either side is false, the result is false.
const isLoggedIn = true;
const hasPermission = false;
console.log(isLoggedIn && hasPermission); // false|| means OR
At least one side must be true.
const hasEmail = true;
const hasPhone = false;
console.log(hasEmail || hasPhone); // trueOnly when both sides are false does || return false.
! means NOT
The NOT operator reverses a boolean.
const isLoggedIn = false;
console.log(!isLoggedIn); // trueThis is useful for conditions:
const isCartEmpty = true;
if (!isCartEmpty) {
console.log("Proceed to checkout");
} else {
console.log("Your cart is empty");
}Converting Values with Boolean()
You can convert a value to a boolean with the Boolean() function.
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("Hello")); // true
console.log(Boolean("")); // falseThis introduces an important JavaScript concept: truthy and falsy values.
Falsy Values
Falsy values behave like false in a boolean context.
The common falsy values are:
false;
0;
"";
null;
undefined;
NaN;Examples:
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean(NaN)); // falseTruthy Values
Most other values are truthy.
console.log(Boolean(1)); // true
console.log(Boolean(-1)); // true
console.log(Boolean("Hello")); // true
console.log(Boolean("0")); // true
console.log(Boolean("false")); // trueNotice that "0" and "false" are truthy because they are non-empty strings.
You will learn truthy and falsy behavior in more depth when you study conditionals.
Practical Example
Imagine a login form.
const email = "asha@example.com";
const password = "secret123";
const hasEmail = email !== "";
const hasPassword = password !== "";
if (hasEmail && hasPassword) {
console.log("Form can be submitted");
} else {
console.log("Please fill out all fields");
}Here:
email !== ""creates a boolean.password !== ""creates a boolean.&&checks that both are true.
The Boolean Object Trap
JavaScript also has a Boolean object wrapper.
Avoid using it.
const primitiveFalse = false;
const objectFalse = new Boolean(false);
console.log(typeof primitiveFalse); // boolean
console.log(typeof objectFalse); // objectThe confusing part is that all objects are truthy.
const objectFalse = new Boolean(false);
if (objectFalse) {
console.log("This still runs");
}Even though the internal value is false, the object itself is truthy.
Use primitive booleans:
const isActive = false;Do not use:
const isActive = new Boolean(false);Naming Boolean Variables
Boolean variables are easier to read when their names sound like yes/no questions.
Good names:
const isLoggedIn = true;
const hasPermission = false;
const canEdit = true;
const shouldShowMenu = false;Less clear:
const login = true;
const permission = false;Names like is, has, can, and should make boolean values easier to understand.
Summary
Booleans represent logical true or false values.
You learned that:
- Boolean values are
trueandfalse. "true"and"false"are strings, not booleans.- Comparisons return booleans.
- Booleans control
ifstatements and other decision-making logic. &&,||, and!combine or reverse boolean values.Boolean()converts values to booleans.- Some values are falsy, while most values are truthy.
- Avoid
new Boolean()because it creates a truthy object.
Next, you will learn about null and undefined, two values that represent absence in different ways.