What are Falsy values in JavaScript?
In JavaScript falsy values are values that when encountered in a Boolean context are considered to be false. They aren't necessarily equal to each other but will evaluate to false when coerced to a Boolean. There are a total of 8 falsy values:
const keywordFalse = false;
const numberZero = 0;
const numberNegativeZero = -0;
const bigIntZero = 0n;
const emptyString = '';
const nullVal = null;
const undefinedVal = undefined;
const notANumber = NaN;
If you're unsure if a value evaluates to falsy you can test it using a simple negated if statement like so:
if (!valueToTest) {
console.log('is falsy');
}
Here's a little extra fun with the logical &&
operator, if the first object is falsy, it returns that object:
if (!valueToTest) {
false && 'hello world'; // false
'' && 'hello world';
}