JavaScript Reference ยท Practical Pages
Truthy and falsy values
Concept page with the complete list of JavaScript falsy values, what counts as truthy, where conversion to boolean happens, and common mistakes with 0 and empty strings.
Wherever JavaScript needs a yes-or-no answer (an if condition, a loop test, the left side of &&, || or ?:) it converts the value to a boolean. A value that becomes false is called falsy; a value that becomes true is truthy. You do not need to memorize rules for every type, because there is a short, complete list of falsy values, and everything else is truthy.
The falsy values
There are exactly eight:
| Value | Type |
|---|---|
false |
boolean |
0 |
number |
-0 |
number |
0n |
BigInt |
"" (also '' and an empty template literal) |
string |
null |
null |
undefined |
undefined |
NaN |
number |
There is also one oddity: document.all, a legacy browser object that is falsy for historical reasons. You will almost never meet it.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Everything else is truthy
That includes values that look "empty" or "negative":
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Every object is truthy, including empty arrays and new Boolean(false). A string is truthy unless it is empty, so "0" and "false" are truthy.
Where the conversion happens
Boolean conversion is used by:
if,while,do...while,forconditions and the?:operator;- the
!operator (!xis the opposite of the converted value, and!!xconverts to a real boolean); &&and||, which test truthiness but return one of the original operands, not a boolean;Boolean(x),Array.prototype.filter(Boolean)and similar callbacks.
It is not used by ==, which follows its own rules: [] == false is true even though [] is truthy. See equality comparison.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Falsy checks and the ?? operator
Truthiness checks cannot tell "no value" from "a valid value that happens to be falsy". The classic bug is a default for a count or a flag:
Press Run (or Ctrl + Enter) to see the console output here.
No output.
?? falls back only for null and undefined. || falls back for every falsy value. Pick the one that matches what "missing" means for that field.
Usage notes
- Check for specific conditions when they matter. Write
items.length > 0orvalue !== undefinedwhen0,""orfalseare valid values. NaNis falsy and unequal to itself, so test numbers withNumber.isNaN().- Empty arrays and objects are truthy. Test
arr.lengthorObject.keys(obj).lengthinstead. "false"(string) is truthy. Parse flags from text with an explicit comparison such asvalue === "true".- Do not use
new Boolean(...). A wrapper object is always truthy. - Optional chaining and
??express "may be missing" more precisely than truthiness; see??and?.. - Logical assignment (
||=,&&=,??=) follows the same rules as the corresponding operators.
Browser support
Truthy and falsy rules are identical in every JavaScript engine. 0n (BigInt) is falsy in all engines that support BigInt.