1. Introduction
While writing JavaScript programs, it is not mandatory for you to provide only true
or false
in Boolean
context. For example, you can use a number or a string as a condition in if
:
if (4) { console.log("passed"); //passed }
A
falsy
value is a value that is consideredfalse
in aBoolean
context.
JavaScript uses type coercion to coerce such values which are not true
or false
in the Boolean
context.
2. JavaScript falsy values
Following are JavaScript falsy
values:
Value | Description |
false | The keyword false. |
0 | The Number zero (and values which are zero such as 0.0, etc., and 0x0). |
-0 | The Number negative zero (and values which are -0 such as 0.0 -0.0, -0x0). |
0n | The BigInt zero (and also 0x0n). Note that there is no BigInt negative zero — the negation of 0n is 0n. |
“”, ”, “ | Empty string value. |
null | the absence of any value. |
undefined | the primitive value undefined. |
NaN | not a number |
document.all |
3. Examples
All the following will result in bypassing of if
block:
if (false) if (0) if (-0) if(0x0) if(0.0) if(-0.0) if ("") if('') if(``) if (null) if (undefined) if (0n) if (NaN)