Booleans & Truthy / Falsy
JavaScript's Boolean type has only two values —
true and false.
But every value in JS is inherently truthy or falsy, and knowing
which is which is the key to writing clean, bug-free conditions.
The Boolean Type
A Boolean is the simplest data type in JavaScript — it
can only hold one of two values: true or
false. These are primitive values (lowercase),
not strings and not objects.
// Boolean literals
const isLoggedIn = true;
const hasDiscount = false;
// typeof a boolean → "boolean"
typeof true; // "boolean"
typeof false; // "boolean"
// Booleans from comparison expressions
const isAdult = 18 >= 18; // true
const isEmpty = "" === ""; // true
const isSame = 5 === 6; // false
// Boolean in an if condition
if (isLoggedIn) {
console.log("Welcome back!");
}true and false are case-sensitive.
Writing True or TRUE will cause a
ReferenceError — they are not valid JavaScript.
The 8 Falsy Values
In JavaScript, every value is either truthy or falsy.
When a value is used in a Boolean context (e.g., inside an if
condition), JS automatically converts it. There are exactly
8 falsy values — everything else is truthy:
// All 8 falsy values — evaluate to false in boolean context
if (false) console.log("nope");
if (0) console.log("nope");
if (-0) console.log("nope");
if (0n) console.log("nope");
if ("") console.log("nope"); // empty string
if (null) console.log("nope");
if (undefined) console.log("nope");
if (NaN) console.log("nope");
// ↑ none of the console.logs above will run
// Common truthy surprises 🚨
if ("0") console.log("truthy!"); // ← non-empty string
if ("false") console.log("truthy!"); // ← non-empty string
if ([]) console.log("truthy!"); // ← empty array is truthy
if ({}) console.log("truthy!"); // ← empty object is truthy
// ↑ all four will print "truthy!""0", "false", [], and {} are all
truthy — even though they may look "empty" or "false-like". An empty array
[] is an object, and all objects are truthy.
Boolean Coercion
You can explicitly convert any value to its Boolean equivalent using
Boolean() or the double-bang operator
!! (double negation). This is called
boolean coercion.
| Value | Boolean() | !! shorthand | Notes |
|---|---|---|---|
| 0 | false | false | Zero is falsy |
| 1 | true | true | Any non-zero number |
| "" | false | false | Empty string is falsy |
| "hello" | true | true | Non-empty string |
| null | false | false | Null is always falsy |
| undefined | false | false | Undefined is always falsy |
| NaN | false | false | Not a Number is falsy |
| [] | true | true | Objects (even empty) are truthy |
| {} | true | true | Objects (even empty) are truthy |
| "0" | true | true | Non-empty string — gotcha! |
// Using Boolean() constructor
Boolean(0); // false
Boolean(1); // true
Boolean(""); // false
Boolean("hello"); // true
Boolean(null); // false
Boolean([]); // true ← empty array!
// Using !! (double negation) — more concise, preferred
!!0; // false
!!1; // true
!!""; // false
!!"hello"; // true
!!null; // false
!![]; // true
// Practical use — convert to boolean flag
const userInput = "John";
const hasInput = !!userInput; // true
const emptyInput = "";
const hasEmpty = !!emptyInput; // falseLogical Operators & Boolean Logic
JavaScript provides three logical operators that work with boolean
(and truthy/falsy) values. Understanding how they return values — not just
true/false — is key
to mastering JavaScript patterns.
true or false. Double !! converts to boolean.
null or undefined. Unlike ||, it respects
0 and "".
// ── AND (&&) — returns first falsy or last value ─────────────
true && "yes"; // "yes" ← all truthy, returns last
false && "yes"; // false ← first falsy
0 && "yes"; // 0 ← 0 is falsy, short-circuits
"ok" && 42; // 42 ← "ok" truthy, returns last
// ── OR (||) — returns first truthy or last value ─────────────
null || "default"; // "default"
undefined || "fallback"; // "fallback"
"Alice" || "Guest"; // "Alice" ← first truthy wins
0 || "zero!"; // "zero!" ← 0 is falsy
// ── NOT (!) ───────────────────────────────────────────────────
!true; // false
!false; // true
!0; // true ← 0 is falsy, so !0 is true
!"hi"; // false ← "hi" is truthy, so !"hi" is false
!!"hi"; // true ← double negation → boolean
// ── Nullish Coalescing (??) ───────────────────────────────────
0 ?? "default"; // 0 ← 0 is NOT null/undefined
"" ?? "default"; // "" ← empty string kept
null ?? "default"; // "default"
undefined ?? "val"; // "val"
|| when you want to fall back for any falsy value (including
0 and ""). Use ?? when you only want to fall back for
null or undefined — this is safer when 0 or
"" are valid values (e.g., a score of 0 should not trigger a default).
Short-Circuit Evaluation
Logical operators in JavaScript are lazy — they stop evaluating as soon as the result is determined. This is called short-circuit evaluation and it's used widely in idiomatic JS code.
// ── Guard clause with && ─────────────────────────────────────
// Only calls sendEmail() if user exists
user && sendEmail(user.email);
// Equivalent long form:
if (user) sendEmail(user.email);
// ── Default value with || ─────────────────────────────────────
const name = inputName || "Guest";
const port = userPort || 3000;
// ── Safer default value with ?? ──────────────────────────────
const score = userScore ?? 0; // 0 only if null/undefined
const label = title ?? "Untitled";
// ── Conditional rendering (React-style) ──────────────────────
const isAdmin = true;
const badge = isAdmin && "Admin Badge"; // "Admin Badge"
// ── Short-circuit prevents errors ────────────────────────────
const user = null;
const city = user && user.address && user.address.city; // null (no error)
// Modern alternative with optional chaining:
const city2 = user?.address?.city; // undefined (cleaner)
Try It — Live Editor
Experiment with truthy/falsy values and boolean operators below. Try changing values and see how they evaluate in boolean context.
Common Patterns & Recipes
These are everyday boolean utilities you'll use constantly in real JavaScript projects:
// ── Check if a value exists (not null/undefined/empty) ───────
const exists = val => val != null && val !== "";
exists("hello"); // true
exists(""); // false
exists(null); // false
// ── Toggle a boolean flag ─────────────────────────────────────
let isOpen = false;
isOpen = !isOpen; // true
isOpen = !isOpen; // false again
// ── Safe property access (avoid TypeError) ───────────────────
const user = null;
const name = user && user.name; // null (no error)
const name2 = user?.name ?? "Guest"; // "Guest" (modern)
// ── Default function parameters with || / ?? ─────────────────
function greet(name) {
const displayName = name || "Guest";
console.log(`Hello, ${displayName}!`);
}
greet("Alice"); // "Hello, Alice!"
greet(); // "Hello, Guest!"
// ── Filter out falsy values from an array ────────────────────
const raw = [0, "hello", null, "", 42, undefined, false, "world"];
const clean = raw.filter(Boolean);
console.log(clean); // ["hello", 42, "world"]
// ── Convert to boolean for storage/API ───────────────────────
const formValue = "on"; // from a checkbox
const isChecked = formValue === "on"; // true0 ?? "default" evaluate to?- The Boolean type has exactly two values:
trueandfalse(case-sensitive, lowercase). - There are exactly 8 falsy values:
false,0,-0,0n,"",null,undefined, andNaN. Everything else is truthy. - Watch out for truthy gotchas:
"0","false",[], and{}are all truthy. - Use
Boolean(val)or!!valto explicitly convert any value to its boolean equivalent. &&returns the first falsy value or the last value;||returns the first truthy value or the last value.- Use
??(nullish coalescing) instead of||when0or""are valid values you want to keep. array.filter(Boolean)is a clean pattern to remove all falsy values from an array in one line.- Short-circuit evaluation with
&&and||is the foundation of most guard clauses and default-value patterns in JavaScript.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.