🚫 Falsy Values in JavaScript
❓ What Are Falsy Values?
In JavaScript, a falsy value is a value that is considered false when evaluated in a Boolean context, such as inside an if condition or a logical operation.
📋 List of Falsy Values
There are exactly 7 falsy values in JavaScript:
- false — the Boolean false
- 0 — the number zero
- -0 — negative zero
- 0n — BigInt zero
- "" — empty string
- null — absence of any value
- undefined — uninitialized variable
- NaN — Not-a-Number
🧪 Examples of Falsy Evaluation
Code Snippet
if (!false) console.log("false is falsy");
if (!0) console.log("0 is falsy");
if (!"") console.log("empty string is falsy");
if (!null) console.log("null is falsy");
if (!undefined) console.log("undefined is falsy");
if (!NaN) console.log("NaN is falsy");✅ Truthy vs. Falsy
Any value that is not falsy is considered truthy. For example:
- true
- Non-zero numbers (e.g., 42, -1)
- Non-empty strings (e.g., "hello")
- Objects (including empty objects and arrays)
- Functions
⚠️ Why Falsy Values Matter?
Understanding falsy values is crucial for writing correct conditions and avoiding unexpected bugs.
Code Snippet
const name = "";
if (name) {
console.log("Name is truthy");
} else {
console.log("Name is falsy"); // This will run
}💡 Quick Tip
Note
Use Boolean(value) or double NOT !!value to explicitly convert any value to its Boolean equivalent.
Code Snippet
console.log(Boolean("")); // false
console.log(!!0); // false
console.log(Boolean("hi")); // true🧾 Summary
- Falsy values coerce to false in Boolean contexts.
- There are exactly 8 falsy values in JavaScript.
- Everything else is truthy.
- Use explicit Boolean conversion to avoid surprises.
>>“Knowing falsy values helps you write cleaner, bug-free code.”