Free lesson · JavaScript Basic to Advance
Booleans and the Truthy/Falsy Trap
What Is a Boolean?
A boolean is the simplest type in JavaScript. It has exactly two possible values: true and false. That's the entire type. Two values, no more.
const isLearning = true;
const isFinished = false;
console.log(isLearning);
console.log(isFinished);
console.log(typeof isLearning);
Booleans exist for one purpose: to represent yes-or-no decisions. Is the user logged in? Is the form valid? Should this button be enabled? Every conditional decision in a program eventually reduces to a boolean.
The reason this Topic exists separately from the others isn't the boolean type itself — that's trivial. The reason is truthy and falsy, two concepts JavaScript uses constantly that confuse almost every learner the first time they encounter them.
Where Booleans Come From
Most of the time, you don't create booleans directly. You get them as the result of comparisons:
console.log(5 > 3); // true
console.log(5 < 3); // false
console.log(5 === 5); // true
console.log("hello" === "hi"); // false
console.log(10 >= 10); // true
console.log(10 !== 10); // false
Each comparison operator returns a boolean. You then use those booleans in conditionals, logical operators, and control flow:
const age = 25;
const hasLicense = true;
const canDrive = age >= 16 && hasLicense;
if (canDrive) {
console.log("Allowed to drive");
} else {
console.log("Not allowed to drive");
}
The expression age >= 16 produces a boolean. The expression age >= 16 && hasLicense combines two booleans into one. The if statement checks that final boolean and runs the matching branch. This is the entire mechanism of decision-making in JavaScript.
Truthy and Falsy: The Confusing Part
Here's where JavaScript surprises you. Inside an if statement, JavaScript does not require a boolean. It accepts any value and figures out whether to treat that value as "true" or "false."
if ("hello") {
console.log("A string is truthy");
}
if (42) {
console.log("A non-zero number is truthy");
}
if ([]) {
console.log("An empty array is truthy");
}
if (0) {
console.log("This will not print");
}
if ("") {
console.log("This will not print either");
}
The first three if statements run their blocks. The last two don't. JavaScript has decided that "hello", 42, and [] are "true enough" to count as true. It has decided that 0 and "" are "false enough" to count as false.
These are called truthy values and falsy values. Truthy means "JavaScript will treat this as true in a boolean context." Falsy means "JavaScript will treat this as false in a boolean context."
The Falsy Values: Memorize These
JavaScript has exactly six falsy values. Every other value in the language is truthy. Memorize this list — it's one of the most useful pieces of knowledge you can have about JavaScript.
graph TD
A[The Six Falsy Values] --> B[false]
A --> C[0]
A --> D["'' empty string"]
A --> E[null]
A --> F[undefined]
A --> G[NaN]
style A fill:#7c3aed,color:#fff
This diagram shows the six falsy values in JavaScript. They are: the literal false, the number zero, an empty string with no characters between the quotes, null, undefined, and NaN. Every other value in JavaScript is truthy.
The list:
false— the boolean false0— the number zero (including-0and0n)""— an empty string (single or double quotes, with nothing between them)nullundefinedNaN
That's it. Six values. Everything else — including empty arrays [], empty objects {}, the string "false", the string "0", negative numbers, Infinity — is truthy.
// These are all falsy
if (!false) console.log("false is falsy");
if (!0) console.log("0 is falsy");
if (!"") console.log("empty string is falsy");
if (!null) console.log("null is falsy");
if (!undefined) console.log("undefined is falsy");
if (!NaN) console.log("NaN is falsy");
// These are all truthy (notice the empty array and empty object)
if ([]) console.log("[] is truthy");
if ({}) console.log("{} is truthy");
if ("false") console.log("the string 'false' is truthy");
if ("0") console.log("the string '0' is truthy");
if (-1) console.log("negative numbers are truthy");
if (Infinity) console.log("Infinity is truthy");
That third line in the truthy block — the string "0" being truthy — surprises everyone. The number 0 is falsy. The string "0" is truthy. They look similar but JavaScript treats them completely differently in boolean contexts.
Interactive animation: Boolean(x) run on the values that confuse people - 0 and "" are falsy, while [], {} and "0" are truthy.
Why Truthy and Falsy Exist
The truthy/falsy system exists to let you write shorter, more flexible code. Instead of explicitly checking whether a string is non-empty or whether a number is non-zero, you can let JavaScript do that check for you:
function greet(name) {
if (name) {
console.log(`Hello, ${name}`);
} else {
console.log("Hello, stranger");
}
}
greet("Olena"); // Hello, Olena
greet(""); // Hello, stranger (empty string is falsy)
greet(null); // Hello, stranger
greet(undefined); // Hello, stranger
That single if (name) check handles four different cases. It's terse and convenient. But it's also the source of bugs when you forget that 0 and "" are falsy:
function describe(value) {
if (value) {
console.log(`The value is ${value}`);
} else {
console.log("No value provided");
}
}
describe("Olena"); // "The value is Olena"
describe(0); // "No value provided" — but 0 IS a value!
describe(""); // "No value provided" — fair
describe(false); // "No value provided" — fair
The describe(0) case is the bug. The number zero is a perfectly valid value, but JavaScript treats it as falsy, so the function reports "No value provided." This kind of bug — known as "the zero problem" — has caused countless hours of debugging across the industry.
When you need to handle this case correctly, check for null and undefined specifically instead of relying on truthiness:
function describeBetter(value) {
if (value !== null && value !== undefined) {
console.log(`The value is ${value}`);
} else {
console.log("No value provided");
}
}
describeBetter(0); // "The value is 0"
describeBetter(""); // "The value is "
describeBetter(null); // "No value provided"
Modern JavaScript has a shorter way to write this check, using the nullish coalescing operator ??, which we'll cover in Module 10. For now, the explicit comparison is fine.
Interactive animation: Falsy but meaningful, like 0 and "", against genuinely absent, like null and undefined - where the zero problem lives.
Common Pitfalls
Warning: The two truthy/falsy traps that bite everyone.
0is falsy, but"0"is truthy. The number zero and the string version of zero behave completely differently. This catches developers constantly when working with form input — a field that contains the string"0"will be treated as truthy, even though the user "typed zero."Empty arrays
[]and empty objects{}are truthy. This surprises everyone. You might assume an empty collection should be falsy, but JavaScript considers any object — even an empty one — to be truthy. To check whether an array is empty, usearray.length === 0. To check whether an object is empty, useObject.keys(obj).length === 0.
Interactive animation: Ask the question you mean - items.length > 0 instead of if (items), because an empty array is truthy.
Why This Matters in Interviews
Interview: "Can you list all the falsy values in JavaScript?" is one of the most common warm-up questions in JavaScript interviews. The expected answer:
false,0,"",null,undefined,NaN. Candidates who forgetNaN(the easiest to miss) or who don't know the list at all signal that they haven't studied the language carefully.Topic 2.8 — Interview Lens: Type Coercion Questions — covers the deeper interview puzzles around truthy/falsy values, including the "zero problem" and questions about how truthiness interacts with the
&&and||operators.
Try It
Exercise: Predict whether each value is truthy or falsy. Then run
Boolean(value)on each to check.Boolean(0) Boolean("0") Boolean("") Boolean(" ") // note: one space Boolean([]) Boolean({}) Boolean(null) Boolean(undefined) Boolean(NaN) Boolean(-1) Boolean(Infinity) Boolean("false") // the string "false" Boolean(0n) // bigint zeroThe pattern after you do this exercise: there are exactly six values that produce
false. Every other value producestrue. Once that pattern is locked in your head, you'll never be confused about truthiness again.