![]() |
VOOZH | about |
In JavaScript, falsy values are those that are evaluated as false when used in a Boolean. Unlike truthy values, falsy values represent "nothingness," "emptiness," or "failure."
If you're writing conditional statements or logical operations, understanding falsy values is very important because these are the only values JavaScript treats as false. Here’s the rule: If a value is not one of JavaScript’s falsy values, it is truthy.
JavaScript has a fixed set of falsy values
The Boolean value false is, naturally, falsy.
The number 0 is falsy, regardless of whether it’s positive or negative zero.
The BigInt value 0n is falsy.
Strings without any characters (empty strings) are falsy, but strings with whitespace or content are truthy.
null represents the intentional absence of any object value and is falsy.
undefined represents a variable that has been declared but not assigned a value. It is falsy.
NaN is the result of invalid or undefined mathematical operations and is falsy.
This is a quirky value used for backward compatibility in web browsers. Although technically an object, it is treated as falsy.
Now let’s see how JavaScript handles these falsy values in action
false is falsy. 0 is falsy. 0 is falsy. 0 is falsy. is falsy. null is falsy. undefined is falsy. NaN is falsy.
Understanding falsy values can help avoid common pitfalls in JavaScript
You can use || to provide a default value if the left operand is falsy.
Guest
Falsy values cause logical operations to short-circuit.
0
Falsy values are often used in if conditions to check for the existence or validity of a value.
Configuration is missing.
Truthy
Truthy
Although both are falsy, they are used differently. null often represents a absence of a value, while undefined usually indicates it is not yet initialize.
If you want to explicitly determine whether a value is falsy, you can use the Boolean function or the double negation operator !!.
false false true false true