Falsy values are those values that get coerced to the boolean type false
in TypeScript and JavaScript.
These are the following falsy
values in TypeScript and JavaScript:
0
false
NaN
"" (empty string)
0n (bigint version of zero)
null
undefined
All the above values when used with an if
conditional gets coerced to the boolean type false
.
To understand it correctly, let's use an if
conditional statement check and use all the above falsy
values.
0
value example
// using the "0" value in an if conditional statement
if (0) {
console.log("if block is executed");
} else {
console.log("else block is executed");
}
// OUTPUT: else block is executed
- In the above code since the
0
value is afalsy
value, the else block will be executed and the output ofelse block is executed
is shown to the console
false
value example
// using the "false" value in an if conditional statement
if (false) {
console.log("if block is executed");
} else {
console.log("else block is executed");
}
// OUTPUT: else block is executed
- In the above code since the
false
value is afalsy
value, the else block will be executed and the output ofelse block is executed
is shown to the console
NaN
value example
// using the "NaN" value in an if conditional statement
if (NaN) {
console.log("if block is executed");
} else {
console.log("else block is executed");
}
// OUTPUT: else block is executed
- In the above code since the
NaN
value is afalsy
value, the else block will be executed and the output ofelse block is executed
is shown to the console
""
(empty string) example
// using the empty string value in an if conditional statement
if ("") {
console.log("if block is executed");
} else {
console.log("else block is executed");
}
// OUTPUT: else block is executed
- In the above code since the
""
value is afalsy
value, the else block will be executed and the output ofelse block is executed
is shown to the console
0n
value example
// using the `0n` value in an if conditional statement
if (0n) {
console.log("if block is executed");
} else {
console.log("else block is executed");
}
// OUTPUT: else block is executed
- In the above code since the
0n
value is afalsy
value, the else block will be executed and the output ofelse block is executed
is shown to the console
null
value example
// using the `null` value in an if conditional statement
if (null) {
console.log("if block is executed");
} else {
console.log("else block is executed");
}
// OUTPUT: else block is executed
- In the above code since the
null
value is afalsy
value, the else block will be executed and the output ofelse block is executed
is shown to the console
undefined
value example
// using the `undefined` value in an if conditional statement
if (undefined) {
console.log("if block is executed");
} else {
console.log("else block is executed");
}
// OUTPUT: else block is executed
- In the above code since the
undefined
is afalsy
value, the else block will be executed and the output ofelse block is executed
is shown to the console
See the above codes live in codesandbox.
That's all 😃!