Lesson 9: Mastering JavaScript Boolean

โ 1. What is a Boolean?
A Boolean is a primitive data type in JavaScript with only two possible values:
true
โ Represents yes, enabled, correct, valid.false
โ Represents no, disabled, incorrect, invalid.
let isLoggedIn = true;
let hasPaid = false;
๐ฌ 2. Real-World Usage of Booleans
Booleans are used for decision making, like:
๐ Login Authentication
let isAuthenticated = true;
if (isAuthenticated) {
console.log("Welcome, user!");
} else {
console.log("Please log in.");
}
๐ณ Payment Status
let paymentSuccessful = false;
if (!paymentSuccessful) {
console.log("Please complete your payment.");
}
๐งพ Form Validation
let nameFilled = true;
let emailFilled = false;
if (nameFilled && emailFilled) {
console.log("Form is ready to submit.");
} else {
console.log("Please complete all fields.");
}
๐ 3. Boolean from Comparisons
Every comparison returns a Boolean:
let age = 25;
let isAdult = age >= 18; // true
You can use these in if
conditions directly.
๐ฆ 4. Boolean Constructor and Coercion
You can explicitly convert a value to Boolean using:
Boolean(0) // false
Boolean(123) // true
Boolean("") // false
Boolean("hi") // true
Boolean(null) // false
Boolean(undefined) // false
Or simply use !!
(double NOT):
!!"hello" // true
!!0 // false
๐ 5. Truthy vs Falsy in JavaScript
โ Truthy:
Any value that isn't falsy is considered truthy.
โ Falsy values (only 8):
These are coerced to false
in Boolean contexts:
Value | Meaning |
false | Literal false |
0 , -0 | Numeric zero |
"" | Empty string |
null | Intentional absence |
undefined | Uninitialized value |
NaN | Not-a-Number |
0n | BigInt zero |
if ("") console.log("Won't run"); // falsy
if ("Hello") console.log("Runs!"); // truthy
โ ๏ธ 6. Real-World Tricky Examples with Falsy/Truthy
๐ 6.1. Checking if a cart has items
let cartItems = 0;
if (cartItems) {
console.log("Proceed to checkout");
} else {
console.log("Your cart is empty");
}
// Will show: "Your cart is empty" โ because 0 is falsy
๐ง Fix:
if (cartItems > 0)
๐ง 6.2. Checking user input
let username = "";
if (username) {
console.log("Welcome, " + username);
} else {
console.log("Please enter your name.");
}
// Will show: "Please enter your name." โ because "" is falsy
๐ฐ 6.3. Checking discount
let discount = null;
if (discount) {
console.log("Discount applied");
} else {
console.log("No discount available");
}
// null is falsy โ "No discount available"
๐ต๏ธ 6.4. Checking existence of object keys
let user = {};
if (user.name) {
console.log("User has a name");
} else {
console.log("Name not set");
}
// If name is undefined โ falsy โ "Name not set"
๐ 7. Logical Operators with Booleans
Operator | Meaning | Returns |
&& | AND | First falsy or last truthy value |
` | ` | |
! | NOT | Inverts truthiness |
true && false // false
false || true // true
!true // false
๐ง 8. Boolean Quirks
๐ต typeof null === "object"
:
typeof null; // "object" (historical bug)
๐ฒ Double NOT Trick:
!!"hello" // true โ convert to boolean
๐ซ Common Mistake:
let status = false;
if (status == "false") {
console.log("false string matched!");
}
// No match โ status is boolean false, not a string.
๐งช 9. Interview-Worthy Edge Cases
๐น Coercion in ==
vs ===
false == 0 // true (coerced)
false === 0 // false (no coercion)
๐น Using truthy/falsy in loops
while ("") {
console.log("Never runs");
}
๐ 10. When to Use:
Scenario | Use Boolean | Notes |
Login Status | โ | true = logged in |
Feature Toggles | โ | Enable/disable logic |
Form Validity | โ | Useful in validations |
Optional Field Presence | โ | !! user.email |
Environment Checks | โ | isDev , isProduction |
๐งฐ 11. Tools & Best Practices
โ Best Practices:
Use
===
instead of==
for strict comparison.Always check for
undefined
/null
explicitly if needed.Use
Boolean(x)
or!!x
for clean coercion.Use truthy/falsy logic sparingly and clearly for readability.
๐ง Mnemonic for Falsy Values:
"0 FUN NAN"
0 โ
0
,-0
,0n
F โ
false
U โ
undefined
N โ
null
N โ
NaN
"" โ (empty string)
Subscribe to my newsletter
Read articles from manoj ymk directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
