2.2 — Truthy & falsy
Yesterday’s fork demanded a boolean. But look at today’s code: if (username) — that’s a string in the condition slot, not a boolean! JavaScript doesn’t complain. Instead, when a condition demands yes/no, it converts any value into one. This is the third arena of coercion that lesson 1.9 promised — and the rule is beautifully simple: six values count as “no”; everything else counts as “yes.”
let username = "";
if (username) {
console.log("Hello, " + username);
} else {
console.log("Please log in");
}Meet the Boolean-izer. Whenever a yes/no is demanded — an if condition, a ! operator — the machine drops the value through this funnel and out comes true or false. No error, no question asked — and the value itself is only READ, never changed. Your job: know which bin each value lands in.
The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”
The official mechanics: conditions perform boolean coercion — the same silent conversion family as lesson 1.9, aimed at true/false.
The complete falsy list is specified in the language standard; it’s genuinely just those six (plus two exotic cousins you’ll never meet: -0 and BigInt’s 0n).
This is why interviewers love the question “name the falsy values” — it is a five-second test of whether someone knows the rulebook or is guessing.
💼 On the job — truthy/falsy bugs are silent. A form that checks if (quantity) treats a legitimate quantity of 0 as “missing”. The user typed zero; the code thought nothing was entered. No error is thrown; the program confidently walks the wrong road. These bugs only reveal themselves under deliberate test inputs — 0, "", a lone space. That is why testers keep a mental list of “edge values”, and it starts suspiciously like the falsy list. You now own that list.
✏️ Quick check 1
Careful, this one bites: type what Boolean("0") evaluates to.
✏️ Quick check 2
The user legitimately ordered zero items. Which function runs — type its name.
let items = 0;
if (items) { ship(); } else { askAgain(); }✏️ Quick check 3
How many falsy values are there in the everyday list? Type the number.
🗣️ Now teach it back
Explain truthy/falsy to a friend: what does the machine do when an if gets a non-boolean, which six values mean “no”, and what’s the sneaky bug with checking if (quantity)?
Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.