Truthy and Falsy in Conditions
You'll learn which values JavaScript treats as "false-like" in if statements, even when they aren't actually the boolean false.
What Is Truthy and Falsy?
When you use a non-boolean value in an if condition, JavaScript automatically coerces it to a boolean. Some values become true (these are truthy), and others become false (these are falsy). This happens behind the scenes using boolean coercion — the same process you've seen with the !! operator.
There are exactly six falsy values in JavaScript:
false(obviously!)0(zero)""(empty string)nullundefinedNaN
Everything else is truthy — including "0", [], {}, and even "false" (a non-empty string).
How It Works
if (0) {
console.log("Won't run — 0 is falsy");
}
if ("hello") {
console.log("Will run — non-empty strings are truthy");
}
let score;
if (score) {
console.log("Won't run — undefined is falsy");
}
if ([]) {
console.log("Will run — empty arrays are truthy!");
}
Think of it like a bouncer checking IDs: six specific values get turned away (falsy), but everything else gets in (truthy). JavaScript doesn't care about "emptiness" for arrays or objects — only those six specific values are blocked.
Common Use Cases
let username = "";
if (username) {
console.log("Welcome, " + username);
} else {
console.log("Please enter a username"); // Runs because "" is falsy
}
Key Takeaway: JavaScript has exactly six falsy values (false, 0, "", null, undefined, NaN) — all other values are truthy in conditions, including empty arrays and objects.