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:
0falseNaN"" (empty string)0n (bigint version of zero)nullundefined
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
0value is afalsyvalue, the else block will be executed and the output ofelse block is executedis 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
falsevalue is afalsyvalue, the else block will be executed and the output ofelse block is executedis 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
NaNvalue is afalsyvalue, the else block will be executed and the output ofelse block is executedis 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 afalsyvalue, the else block will be executed and the output ofelse block is executedis 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
0nvalue is afalsyvalue, the else block will be executed and the output ofelse block is executedis 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
nullvalue is afalsyvalue, the else block will be executed and the output ofelse block is executedis 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
undefinedis afalsyvalue, the else block will be executed and the output ofelse block is executedis shown to the console
See the above codes live in codesandbox.
That's all 😃!