“Truthy and Falsy: The Logic You Didn’t Know You Were Using”
Praveena durai

Praveena durai @praveena_2109

About: passionate about web development

Location:
chennai,india
Joined:
Jun 7, 2025

“Truthy and Falsy: The Logic You Didn’t Know You Were Using”

Publish Date: Jul 11
0 0

Hello everyone!
Today we are going to see about the topic of truthy and falsy
Introduction: Everyday Logic in Code

  • Start with a relatable hook (example: "Have you ever seen JavaScript behave strangely in an if condition?")
  • Introduce the idea that JavaScript doesn’t just use true and false—it interprets many values in a boolean way.
  • Set the tone that this blog will uncover this “hidden logic”.

What Does ‘Truthy’ and ‘Falsy’ Mean?

  • Define “truthy” = values that behave like true
  • Define “falsy” = values that behave like false
  • Mention it’s part of type coercion in JavaScript

The 7 Falsy Values in JavaScript

List and explain briefly:

  1. false
  2. 0
  3. "" (empty string)
  4. null
  5. undefined
  6. NaN
  7. document.all (rare case)

Example:

js
if (0) {
console.log("This won't run, because 0 is falsy");
}

Examples of Truthy Values

  • Almost everything else is truthy: non-empty strings, arrays, objects, numbers other than 0
  • Show some examples:

js
if ("hello") console.log("Truthy string");
if ([]) console.log("Truthy empty array");

Why This Matters in Real Projects

  • Show where truthy/falsy values sneak in:

    • Conditionals
    • Logical operators ||, &&
    • Ternary operators
    • Default value fallbacks

Example:

js
let username = userInput || "Guest";

Common Mistakes Developers Make

  • Using == instead of ===
  • Assuming empty arrays or objects are falsy (they’re truthy!)
  • Forgetting NaN is falsy

How to Check If a Value is Truthy or Falsy

  • Use Boolean() or double !!: js console.log(Boolean("hello")); // true console.log(Boolean("")); // false

Conclusion: Logic You Now Understand

  • Wrap up with how knowing truthy/falsy can help avoid bugs
  • Encourage testing values and using strict equality when needed

Comments 0 total

    Add comment