JavaScript Course Lesson 8 of 16
The Codex / JavaScript / Booleans & Comparison

Booleans & Comparison

true/false, == vs === (the most important JS lesson), the falsy list, and logical operators.

What it is, in plain English: A boolean is simply true or false — yes or no, on or off. JavaScript uses booleans to make decisions. The critical lesson for every JavaScript learner: always use === (triple equals) for comparison, never == (double equals). Triple equals checks value AND type; double equals does type conversion first and produces results that surprise everyone.

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

Always use === (triple equals) for comparison. Never use == (double equals). This is the single most important habit to build 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 case

The == 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 to

The 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", [], {}).

false
the boolean false itself
0
the number zero (also -0 and 0n)
""
empty string (any empty string)
null
null value
undefined
undefined value
NaN
Not a Number
// 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)
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.