Truthy & Falsy
Values in JavaScript
Understand how JavaScript evaluates every value as true or false in boolean contexts, and avoid the common pitfalls that trip up even experienced developers.
What Is a Boolean Context?
JavaScript frequently needs to decide: is this value true or false? This happens whenever a value is used in a place that expects a boolean: if statements, while loops, logical operators, and ternary expressions.
The Core Rule: Falsy Is the Short List
There are exactly 8 falsy values in JavaScript. Every other value in the entire language is truthy. Memorize the falsy list, and you automatically know the truthy side.
Why Truthy/Falsy Matters
Type Coercion Under the Hood
When JavaScript encounters a value in a boolean context, it internally calls the abstract operation ToBoolean. You can observe this behavior explicitly using the Boolean() function or the double-NOT operator !!.
The Falsy Eight: A Closer Look
Truthy vs. true, Falsy vs. false
This is a crucial distinction. Truthy and falsy describe how a value behaves in a boolean context. They are not the same as the literal boolean values true and false.
| Truthy / Falsy | true / false | |
|---|---|---|
| What is it? | A behavior category | Actual boolean values |
| Type | Any type (string, number, etc.) | Boolean only |
| Example | "hello" is truthy | true is true |
| Strict equality | "hello" === true is false | true === true is true |
Surprisingly Truthy Values
These values look like they should be falsy, but they're actually truthy. They trip up beginners and experienced developers alike:
The Coercion Flow
Logical Operators && and ||
JavaScript's || and && don't return true/false: they return one of the original operands. They use truthy/falsy to decide which one:
How to Apply This Knowledge
Example 1: Simple Condition
A function receives a user's name and greets them. What happens with different inputs?
Example 2: Default Values with ||
Using || to provide fallback defaults is a classic JavaScript pattern:
Example 3: Array & Object Gotchas
A common mistake is checking if an array or object is "empty" using a truthy check:
Common Mistakes
|| vs. ??: Comparison
| Expression | || "default" | ?? "default" |
|---|---|---|
| undefined | "default" | "default" |
| null | "default" | "default" |
| 0 | "default" | 0 |
| "" | "default" | "" |
| false | "default" | false |
| "hello" | "hello" | "hello" |
Question 1 of 5
Which of the following values is falsy in JavaScript?
Question 2 of 5
What does the expression "" || "fallback" evaluate to?
Question 3 of 5
What is the output of Boolean([])?
Question 4 of 5
A function uses port = port || 3000. A user passes 0 as the port. What value does port become?
Question 5 of 5
Which operator should you use when you want a default value only for null and undefined, but NOT for 0 or ""?