Truthy and Falsy Values
Truthy and falsy (sometimes written falsey) values
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false, 0, -0, 0n, "", null, undefined, and NaN.
JavaScript uses type coercion in Boolean contexts.
The following table provides a list of JavaScript falsy values:
| Value | Description |
|---|---|
false | The keyword false. |
0, 0.0, 0x0 | The Number zero. |
-0, -0.0, -0x0 | The Number negative zero. |
0n, 0x0n | The BigInt zero. |
"", '', `` | Empty string value. |
null | null — the primitive value. |
undefined | undefined — the primitive value. |
NaN | NaN — not a number. |
Examples of falsy values in JavaScript (which will be coerced to true in boolean contexts, and thus execute the if block):
if (false) { // Not reachable } if (null) { // Not reachable } if (undefined) { // Not reachable } if (0) { // Not reachable } if (-0) { // Not reachable } if (0n) { // Not reachable } if (NaN) { // Not reachable } if ("") { // Not reachable }
Examples of falsy values in JavaScript (which will be coerced to true in boolean contexts, and thus execute the while block):
while (false) { // Not reachable } while (null) { // Not reachable } while (undefined) { // Not reachable } while (0) { // Not reachable } while (-0) { // Not reachable } while (0n) { // Not reachable } while (NaN) { // Not reachable } while ("") { // Not reachable }