Introduction
In many programming languages, not all values are strictly Boolean (true or false), but can be interpreted as such in a Boolean context. These values are known as “truthy” (evaluated as true) or “falsy” (evaluated as false).
Understanding truthy and falsy values is essential for writing concise conditionals, avoiding bugs in control flow, and mastering type coercion behavior in dynamic or loosely typed languages such as JavaScript, Python, Ruby, and others.
What Are Truthy and Falsy Values?
- A truthy value is any value that evaluates to
truein a Boolean context, even if it’s not the Booleantrueitself. - A falsy value is any value that evaluates to
falsein a Boolean context, even if it’s not explicitly the Booleanfalse.
Boolean Contexts:
Examples of where values are coerced to Boolean:
iforwhileconditions- Logical operators (
&&,||) - Ternary expressions
- Boolean conversion (
!!valuein JavaScript orbool(value)in Python)
Truthy / Falsy in Different Languages
JavaScript
Falsy Values:
false
0
-0
0n // BigInt zero
"" // empty string
null
undefined
NaN
Everything else is truthy, including:
"0"
"false"
[]
{}
function(){}
Python
Falsy Values:
False
None
0, 0.0, 0j
""
[]
{}
set()
range(0)
Everything else is truthy, including:
"0"
[0]
(0,)
{"key": None}
Ruby
Only two falsy values:
false
nil
Everything else, including 0 and "", is truthy.
Checking for Truthiness
JavaScript
if (value) {
// Executes if value is truthy
}
if (!value) {
// Executes if value is falsy
}
Convert any value explicitly to a Boolean using:
Boolean(value)
!!value
Python
if value:
# Runs if truthy
if not value:
# Runs if falsy
Convert explicitly:
bool(value)
Examples
JavaScript Truthy
if ("hello") {
console.log("This is truthy");
}
Python Falsy
if not []:
print("Empty list is falsy")
Real-World Use Cases
Default Parameters
function greet(name) {
let user = name || "Guest"; // if name is falsy, use "Guest"
console.log("Hello, " + user);
}
Short-circuit Evaluation
username = input or "anonymous"
If input is falsy (e.g., empty string), fallback to "anonymous".
Defensive Programming
if (data && data.items && data.items.length > 0) {
// Safe from runtime errors
}
Removing Falsy Values
JavaScript:
let cleaned = [0, 1, "", null, 2, false].filter(Boolean)
// → [1, 2]
Python:
cleaned = list(filter(bool, [0, 1, "", None, 2, False]))
# → [1, 2]
Falsy Values Comparison
| Value | JavaScript | Python | Ruby |
|---|---|---|---|
false | Falsy | Falsy | Falsy |
0 | Falsy | Falsy | Truthy |
"" | Falsy | Falsy | Truthy |
null / None | Falsy | Falsy | Falsy (nil) |
undefined | Falsy | N/A | N/A |
NaN | Falsy | N/A | N/A |
[] | Truthy | Falsy | Truthy |
{} | Truthy | Falsy | Truthy |
Pitfalls and Gotchas
| Mistake | Why It Happens |
|---|---|
"0" is truthy in JS | Non-empty string, even if it looks falsey |
[] is truthy in JS, falsy in Python | Language inconsistency |
NaN, undefined, null are all falsy in JS | Easy to confuse in logic checks |
| Using ` |
Example:
let count = 0;
let displayCount = count || 10; // → 10, not 0 (unintended)
Fix:
let displayCount = (count !== undefined && count !== null) ? count : 10;
Or use nullish coalescing (??):
let displayCount = count ?? 10; // works correctly for 0
When to Use Explicit Booleans
Use === true or === false (or is True in Python) only when you specifically want to check against Boolean values, not truthiness in general.
if (flag === true) {
// Only if flag is actually `true`, not "truthy"
}
Truthy / Falsy in Functional Languages
Languages like Haskell and Elm do not support truthy/falsy—conditions must be explicitly Boolean. This enhances type safety, reducing bugs due to implicit coercion.
if x then ... -- x must be of type Bool
Truthy/Falsy in SQL
SQL uses three-valued logic: TRUE, FALSE, and UNKNOWN.
WHERE NOT (deleted_at IS NOT NULL)
Falsy behavior includes:
NULLdoes not behave likefalseNULLinWHEREclauses must be handled explicitly
Summary of Boolean Coercion
| Language | Coerces types to Boolean? | Truthy/Falsy distinction? |
|---|---|---|
| JavaScript | Yes | Yes |
| Python | Yes | Yes |
| Ruby | Yes | Yes (but only 2 falsy) |
| C | Yes (0 is falsy) | Yes |
| Haskell | No (Bool only) | No |
Conclusion
The truthy/falsy concept simplifies conditional logic but also introduces implicit type coercion, which can lead to subtle bugs. Understanding how your language interprets different values in Boolean contexts helps you write defensive, predictable, and readable code.
Always consider:
- What counts as falsy?
- Should I use short-circuit logic or explicit Boolean comparison?
- Do I need
nullish coalescinginstead of||?
With these principles in mind, you can harness the power of truthy/falsy logic without falling into its traps.
Related Keywords
- Boolean Coercion
- Conditional Evaluation
- Falsey Value
- Logical Operator
- Null Coalescing
- Short Circuit Evaluation
- Truthy Value
- Type Coercion
- Undefined Behavior
- Weak Typing
- Zero Value









