JavaScript sometimes winks at you and says:
“This isn’t technically false… but let me think about it.” 🤨
This article is written to help you understand JavaScript’s psychology around true and false.
In this content, we will:
- Explore the Truthy & Falsy concept from basics to mastery
- See lots of code examples under every heading
- Explain each example line by line, in human language
- Clean up real-world bug-producing traps one by one 🧹
If you’re ready, let’s begin… grab your coffee ☕🚀
🧠 What Are Truthy & Falsy? (Truly in Human Language)
In JavaScript, not everything is strictly true or false.
But JavaScript often asks this question:
“Does this feel true or not?”
That’s where truthy and falsy come in.
📌 Definition:
- Falsy → values that behave like
false - Truthy → values that behave like
true
Important point 👇
Even if a value is not a boolean, JavaScript converts it to boolean inside conditions.
if ("hello") {
console.log("It worked");
}
🧠 Explanation:
"hello"is not a boolean- But JavaScript interprets it as true
❌ Falsy Values (The Golden List – MEMORIZE THIS)
There are ONLY 8 falsy values in JavaScript. Period.
false
0
-0
0n
""
null
undefined
NaN
🧠 Easy rule to remember:
Everything outside this list is truthy.
🔬 Mini Test
if ("") {
console.log("You won’t see me");
} else {
console.log("Because it’s an empty string");
}
📌 Explanation:
""→ empty string- Empty string → falsy
- That’s why the
elseblock runs
✅ Truthy Values (Surprising Facts)
Everything not on the falsy list is truthy.
Yes… everything 😎
🤯 Most Surprising Truthy Examples
if ([]) console.log("Empty array but true");
if ({}) console.log("Empty object but true");
if (" ") console.log("Whitespace but true");
if ("0") console.log("String zero but true");
if ("false") console.log("String false but true");
🧠 Explanation:
- Array exists → truthy
- Object exists → truthy
- Has characters → truthy
JavaScript says:
“It may be empty… but it exists.”
⚠️ Most Common Traps (Bug-Producing Zone)
🚨 Trap 1: Empty Array Check
const items = [];
if (items) {
console.log("List exists");
}
❌ This always runs.
🧠 Why?
- Because
[]→ truthy
✅ Correct check:
if (items.length > 0) {
console.log("Actually not empty");
}
🚨 Trap 2: The Number 0 Problem
let score = 0;
if (score) {
console.log("Score exists");
}
❌ This does not run.
🧠 Because:
0→ falsy
✅ Safe check:
if (score !== null && score !== undefined) {
console.log("Score is defined");
}
🚨 Trap 3: Default Values with ||
const username = inputName || "Guest";
🧠 What happens?
- If
inputNameis falsy →"Guest"
But:
const score = userScore || 10;
If userScore = 0?
👉 Result becomes 10 😬
✅ Modern Solution: Nullish Coalescing
const score = userScore ?? 10;
🧠 ?? only checks:
nullundefined
It doesn’t fight with 0 ❤️
🧪 Forcing Boolean (Take Control)
🔹 Boolean() Function
Boolean(0); // false
Boolean("JS"); // true
Boolean([]); // true
Boolean(null); // false
🧠 Readable but a bit verbose.
🔹 The !! Operator (JS Ninja Technique 🥷)
!!"hello" // true
!!0 // false
!![] // true
🧠 Explanation:
- First
!→ negates - Second
!→ negates again - Result → a clean boolean
🧠 Real-Life Use Cases
✔️ Form Validation
if (email && password) {
submitForm();
}
📌 Both values must be truthy for the form to submit.
✔️ Conditional Rendering Logic
isLoggedIn && showDashboard();
🧠 Short, clean, readable.
🚦 Most Common Mistakes
❌ Ignoring 0 as a valid value
❌ Assuming empty arrays are falsy
❌ Using || for everything
❌ Memorizing truthy/falsy without understanding
🏆 Golden Rules (Save These)
✨ Memorize the falsy list
✨ Be explicit in critical checks
✨ Prefer ?? over || when needed
✨ Readable code > short code
💬 Final Words
Truthy & Falsy is one of JavaScript’s smallest yet most dangerous topics.
Once you understand it:
- Bugs decrease
- Conditions become clearer
- JavaScript surprises you less 😌
And yes…
you officially level up as a JavaScript developer 😎💻
