JavaScript — Booleans
What are Booleans?
A boolean is one of two values: true or false. They're used for conditions, flags, and logical decisions.
let isRaining = true;
let isSunny = false;
Creating Booleans
// Direct values
const a = true;
const b = false;
// From comparisons
const x = 5 > 3; // true
const y = 10 === 5; // false
// From function returns
const isEmpty = arr.length === 0; // true or false
Comparison Operators
These return booleans:
| Operator | Meaning | Example |
|---|---|---|
=== | Strict equality | 5 === 5 → true |
!== | Strict inequality | 5 !== "5" → true |
== | Loose equality | 5 == "5" → true (avoid!) |
!= | Loose inequality | 5 != "5" → false (avoid!) |
> | Greater than | 5 > 3 → true |
< | Less than | 5 < 3 → false |
>= | Greater or equal | 5 >= 5 → true |
<= | Less or equal | 5 <= 3 → false |
Always use === and !== — they check both value and type:
0 == false // true (loose — misleading)
0 === false // false (strict — correct)
"" == false // true (loose — misleading)
"" === false // false (strict — correct)
null == undefined // true (loose)
null === undefined // false (strict)
Logical Operators
| Operator | Meaning | Example |
|---|---|---|
&& | AND | true && false → false |
|| | OR | true || false → true |
! | NOT | !true → false |
AND (&&)
Both must be true:
const age = 25;
const hasID = true;
if (age >= 21 && hasID) {
console.log("Entry allowed");
}
OR (||)
At least one must be true:
const isWeekend = false;
const isHoliday = true;
if (isWeekend || isHoliday) {
console.log("Day off!");
}
NOT (!)
Flips true to false and vice versa:
const isLoggedIn = false;
if (!isLoggedIn) {
console.log("Please log in");
}
Truthy and Falsy Values
JavaScript automatically converts non-boolean values to true/false:
Falsy Values (convert to false)
false
0
-0
0n // BigInt zero
"" // empty string
null
undefined
NaN
document.all
Truthy Values (convert to true)
true
1 // any non-zero number
-1
"hello" // any non-empty string
[] // empty array
{} // empty object
function() {}
Using Truthiness
const name = "";
if (name) {
console.log("Hello, " + name);
} else {
console.log("Name is empty"); // This runs
}
// Safer: check explicitly
if (name !== "") {
console.log("Hello, " + name);
}
Boolean Coercion
// Boolean() converts any value to boolean
Boolean(0) // false
Boolean("") // false
Boolean(null) // false
Boolean(undefined) // false
Boolean(NaN) // false
Boolean(1) // true
Boolean("hello") // true
Boolean([]) // true
Boolean({}) // true
// Double NOT (!!) converts to boolean quickly
!!0 // false
!!"hello" // true
!![] // true
Short-Circuit Evaluation
// OR: returns first truthy value
const name = user.name || "Anonymous";
// AND: returns first falsy value
const result = isValid && processData();
// Nullish coalescing: only checks null/undefined
const count = inputCount ?? 0; // 0 if inputCount is null/undefined
Common Patterns
// Toggle
let isOn = false;
isOn = !isOn; // true
// Conditional execution
const admin = true;
admin && deleteItem(); // deleteItem() only runs if admin is true
// Default values
const color = userColor || "blue";
// Guard clause
if (!isLoggedIn) return redirectToLogin();
Mini Practice
- Create variables and compare them with
===and== - Write a condition that checks if a number is between 1 and 100
- Use
&&,||, and!in combinations - Test truthy and falsy values with
Boolean() - Use short-circuit evaluation to set a default value
Up Next
Next: Strings →
Related Topics
Frequently Asked Questions about Booleans
What is Booleans in JavaScript?
Booleans is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Booleans?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Booleans.
Why is Booleans important in JavaScript?
Booleans is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.