Lesson 46 of 296
Truthy and falsy values
What you will learn
- The complete list of falsy values
- Surprising truthy values
- Where truthiness is used
- When relying on truthiness causes bugs
On this page
The 8 falsy values
When JavaScript needs a boolean (in if, while, !, &&, ||), it converts the value. Only these 8 values become false:
| Value | Type |
|---|---|
false | boolean |
0 | number |
-0 | number |
0n | bigint |
"" (empty string) | string |
null | null |
undefined | undefined |
NaN | number |
Everything else is truthy.
Surprising truthy values
"0" truthy (a non-empty string)
"false" truthy (a non-empty string)
" " truthy (a space is a character)
[] truthy (every object is truthy)
{} truthy
function(){} truthy
-1 truthy (only 0 is falsy)
Infinity truthy
new Boolean(false) truthy (an object, even though it wraps false)Where truthiness is used
if (value) { }
while (value) { }
!value
value && other
value || fallback
value ? a : b
Boolean(value)
!!valueChecking for empty things correctly
| What you want to check | Wrong | Right |
|---|---|---|
| Is the array empty? | if (!list) (an empty array is truthy) | if (list.length === 0) |
| Is the object empty? | if (!obj) | if (Object.keys(obj).length === 0) |
| Is the string empty? | - | if (text === "") or if (!text) |
| Is the value missing? | if (!value) (also catches 0 and "") | if (value == null) or value === undefined |
| Is the number valid? | if (!n) (rejects 0) | if (Number.isFinite(n)) |
The classic bug
function showPrice(price) {
if (!price) {
return "Price not available";
}
return `£${price}`;
}
showPrice(0); "Price not available" (wrong: the item is free)Fix:
if (price === undefined || price === null) { ... }or
if (price == null) { ... }The same bug appears with || defaults (Lesson 35): quantity || 1 turns a real 0 into 1. Use ??.
Where truthiness is fine
When 0, "" and false are not valid values for that variable, truthiness is short and readable:
if (user) { ... } user is an object or null
if (errorMessage) { ... } a message or empty string
items.filter(Boolean) removes all falsy items from an arrayfilter(Boolean) passes each item to Boolean(), keeping only truthy ones:
[0, "a", "", null, "b", undefined].filter(Boolean) ["a", "b"]Be careful: it also removes 0.
Common mistakes
- Thinking
[],{}or"0"are falsy. - Using
if (!value)when 0 or empty string are valid. - Using
filter(Boolean)on numbers and losing zeros.
How to say it in an interview
"There are exactly eight falsy values: false, zero, negative zero, 0n, empty string, null, undefined and NaN. Everything else is truthy, including empty arrays, empty objects and the strings '0' and 'false'. Truthiness is convenient, but when zero or an empty string are valid values I check for null or undefined explicitly to avoid bugs."
Key words
| Word | Meaning |
|---|---|
| Truthy | A value that counts as true in a condition |
| Falsy | One of the 8 values that count as false |
Boolean(x) / !!x | Convert a value to a real boolean |
filter(Boolean) | Remove falsy values from an array |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.