A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.
JavaScript uses type conversion to coerce any value to a Boolean in contexts that require it, such as conditionals and loops.
There are 8 falsy values:
false |
The keyword false |
0 |
The number zero |
-0 |
The number negative zero |
0n |
BigInt, when used as a boolean, follows the same rule as a Number. 0n is falsy. |
"" |
Empty string value |
null | null - the absence of any value |
undefined | undefined - the primitive value |
NaN | NaN - not a number |
Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot. This slot only exists in document.all
and cannot be set using JavaScript.
Examples
Examples of falsy values in JavaScript (which are coerced to false in Boolean contexts, and thus bypass the if
block):
if (false) if (null) if (undefined) if (0) if (-0) if (0n) if (NaN) if ("")
The logical AND operator, &&
If the first object is falsy, it returns that object
false && "dog" // ↪ false 0 && "dog" // ↪ 0
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of ' ToBoolean abstract operation' in that specification. |