Tutorials JavaScript Booleans & Truthy / Falsy
Ch 9 / 78
Chapter 11 Beginner 6 min read JavaScript

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.

What you'll learn
The Boolean type — true and false primitives
All 8 falsy values in JavaScript
What makes a value truthy
Boolean coercion with Boolean() and !! operator
Logical operators &&, ||, ! and short-circuit evaluation
Practical patterns — default values, guard clauses

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.

true
Boolean True
Represents an affirmative condition, a yes, or a passing check. Used when something is on, enabled, or present.
false
Boolean False
Represents a negative condition, a no, or a failing check. Used when something is off, disabled, or absent.
JavaScript — Boolean Basics
// 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!");
}
Note
The keywords 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:

Falsy Values (8 total)
false The Boolean false literal itself
0 The number zero
-0 Negative zero (yes, it's different!)
0n BigInt zero
"" Empty string (single, double, or backtick)
null Intentional absence of a value
undefined Variable declared but not assigned
NaN Not a Number (result of invalid math)
Truthy Examples (everything else)
true The Boolean true literal
1, -1, 3.14 Any non-zero number
"hello" Any non-empty string
"0", "false" Non-empty strings — even "0" and "false"!
[] Empty array (objects are always truthy)
{} Empty object (still truthy)
function(){} Any function reference
Infinity Positive and negative infinity
JavaScript — Falsy Values in Action
// 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!"
Common Gotcha
"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!
JavaScript — Boolean Coercion
// 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; // false

Logical 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.

&&
AND — Logical AND
Returns the first falsy value, or the last value if all are truthy. Used for guard conditions.
true && "yes" → "yes"
||
OR — Logical OR
Returns the first truthy value, or the last value if all are falsy. Classic for default values.
null || "default" → "default"
!
NOT — Logical NOT
Inverts a boolean value. Always returns true or false. Double !! converts to boolean.
!true → false · !!0 → false
??
Nullish Coalescing (ES2020)
Returns the right side only if the left side is null or undefined. Unlike ||, it respects 0 and "".
0 ?? "default" → 0
JavaScript — Logical Operators
// ── 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"
Tip — || vs ??
Use || 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.

JavaScript — Short-Circuit Patterns
// ── 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.

Live Editor
✎ Editor
👁 Preview

Common Patterns & Recipes

These are everyday boolean utilities you'll use constantly in real JavaScript projects:

JavaScript — Boolean Recipes
// ── 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"; // true
⚡
Free Resource
Download our JavaScript Cheat Sheet
Booleans, truthy/falsy, operators & coercion — all on one page.
Get Sheet
Quick Check
Which of the following is falsy in JavaScript?
Quick Check
What does 0 ?? "default" evaluate to?
Chapter Summary
  • The Boolean type has exactly two values: true and false (case-sensitive, lowercase).
  • There are exactly 8 falsy values: false, 0, -0, 0n, "", null, undefined, and NaN. Everything else is truthy.
  • Watch out for truthy gotchas: "0", "false", [], and {} are all truthy.
  • Use Boolean(val) or !!val to 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 || when 0 or "" 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

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel