Booleans & Comparison
true/false, == vs === (the most important JS lesson), the falsy list, and logical operators.
Making decisions: true, false, and comparison
Programs make decisions constantly — "is the user logged in?", "is the price above $100?", "did the form field get filled in?" The answer to these questions is always either true or false. That's a boolean.
The most important rule in JavaScript
Here's why:
// === checks: same value AND same type?
console.log(5 === 5); // true ✓ — both are the number 5
console.log(5 === "5"); // false ✓ — one is a number, one is a string
console.log(0 === false); // false ✓ — 0 is a number, false is a boolean
// == checks: same value AFTER converting types (dangerous!)
console.log(5 == "5"); // true ✗ — JS converted "5" to 5
console.log(0 == false); // true ✗ — JS converted false to 0
console.log("" == false); // true ✗ — both became 0
console.log(null == undefined); // true ✗ — special caseThe == operator converts types before comparing — this is called
type coercion. The results are often not what you'd expect.
Triple === never converts — it compares exactly as-is. Use it always.
Comparison operators
let age = 25;
console.log(age === 25); // true — equal to
console.log(age !== 18); // true — NOT equal to (use !== not !=)
console.log(age > 18); // true — greater than
console.log(age < 30); // true — less than
console.log(age >= 25); // true — greater than OR equal to
console.log(age <= 24); // false — less than OR equal toThe falsy list — values that act like false
JavaScript has exactly 6 falsy values — values that behave like
false in an if condition. Everything else is truthy
(including "0", "false", [], {}).
false0""nullundefinedNaN// These DON'T run (falsy):
if (0) { console.log("nope"); }
if ("") { console.log("nope"); }
if (null) { console.log("nope"); }
if (undefined) { console.log("nope"); }
// These DO run (truthy — might surprise you):
if ("0") { console.log("yes — '0' is a non-empty string"); }
if ([]) { console.log("yes — empty array is truthy"); }
if ({}) { console.log("yes — empty object is truthy"); }
if (-1) { console.log("yes — any non-zero number is truthy"); }This matters for checking if a value exists:
let username = getUserInput(); // might be "" or null or "Alice"
// ✓ Common pattern — check if username is "truthy" (non-empty, non-null)
if (username) {
console.log(`Welcome, ${username}!`);
} else {
console.log("Please enter a username.");
}Logical operators: AND, OR, NOT
// && (AND) — true only if BOTH sides are true
console.log(true && true); // true
console.log(true && false); // false
console.log(5 > 3 && 10 > 7); // true
// || (OR) — true if AT LEAST ONE side is true
console.log(false || true); // true
console.log(false || false); // false
console.log(5 > 10 || 1 > 0); // true
// ! (NOT) — flips true to false and vice versa
console.log(!true); // false
console.log(!false); // true
console.log(!0); // true (0 is falsy, !falsy = true)
console.log(!"hello"); // false ("hello" is truthy, !truthy = false)Official Sources
- MDN Web Docs — Equality comparisons and sameness
- MDN Web Docs — Nullish coalescing (??)
- MDN Web Docs — Optional chaining (?.)
- ECMAScript 2024 — Abstract Equality Comparison (§7.2.15)
- ECMAScript 2024 — Strict Equality Comparison (§7.2.16)
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.