A Falsy value is a value that is considered false when encountered in a boolean context.
The following table provides a complete list of Javascript falsy values:
Value | Description |
false | keyword false |
0 | number 0, 0.0, 0*0, 0*n |
-0 | number -0, -0.0, -0*0 |
0n | BigInt |
"", '', `` | Empty string |
null | absence of any value |
undefined | primitive value |
NaN | not a number |
Examples:
if (false){
//Not reachable
}
if (0 || -0){
//Not reachable
}
if (0n){
//Not reachable
}
if (""){
//Not reachable
}
if (null || undefined || NaN){
//Not reachable
}
Truthy
A Truthy is a value that is considered true when encountered in boolean context. All values are true unless they are defined as false i.e., all values are truthy except falsey such as null, undefined, NaN, false, 0, -0, 0n, " ".
if (true){
// reachable
}
if (1 || 2){ // any number other that 0 and -0
// reachable
}
if ("0"){
// reachable
}
if ("false"){
// reachable
}
if (Infinity || -Infinity){
// reachable
}
ย