Lesson 9: Mastering JavaScript Boolean

manoj ymkmanoj ymk
4 min read

โœ… 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:

ValueMeaning
falseLiteral false
0, -0Numeric zero
""Empty string
nullIntentional absence
undefinedUninitialized value
NaNNot-a-Number
0nBigInt 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

OperatorMeaningReturns
&&ANDFirst falsy or last truthy value
``
!NOTInverts 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:

ScenarioUse BooleanNotes
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)

0
Subscribe to my newsletter

Read articles from manoj ymk directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

manoj ymk
manoj ymk