Lesson 3: Truthy and Falsy Values

Learning Objectives

By the end of this lesson, learners will:

  • Understand what “truthy” and “falsy” mean in JavaScript
  • Know which values are considered falsy
  • Learn how truthy/falsy values affect conditional statements (if, while, etc.)
  • Avoid common mistakes caused by assuming values are always true or false

What Are Truthy and Falsy Values?

In JavaScript, every value is either:

  • Truthy: Evaluates as true in a Boolean context
  • Falsy: Evaluates as false in a Boolean context

This matters especially in conditions, like:

if (value) {
  // This block runs if value is truthy
} else {
  // This block runs if value is falsy
}

What Are Falsy Values?

JavaScript has exactly 7 values that are considered falsy:

Falsy ValueDescription
falseThe Boolean false
0The number zero
-0Negative zero (rare)
""Empty string
nullRepresents “no value”
undefinedDeclared but not assigned
NaN“Not a Number” (e.g. invalid math)

Example:

let username = "";

if (username) {
  console.log("Welcome back!");
} else {
  console.log("Please enter your name.");
}

Since username is an empty string (a falsy value), the else block runs.


What Are Truthy Values?

Anything that is not falsy is truthy!

That includes:

  • Any non-zero number (e.g. 42, -7)
  • Non-empty strings (e.g. "hello")
  • Objects (even empty ones: {})
  • Arrays (even empty: [])
  • Functions

Example:

let age = 25;

if (age) {
  console.log("Age is set!");
}

age = 25 is a truthy value, so the message prints.


Real-World Use Case

Let’s say you’re checking if a user typed something into a form:

let input = prompt("Enter your name:");

if (input) {
  console.log("Thanks, " + input + "!");
} else {
  console.log("You didn't enter anything.");
}

🧠 If the user presses “Cancel” or leaves it blank, input is falsy, and the else message appears.


Common Pitfalls

1. Mistaking "0" (string) as falsy

📌 "0" is truthy because it’s a non-empty string.

if ("0") {
  console.log("This is truthy!"); // ✅ Runs!
}

2. Forgetting that null and undefined are falsy

These are often returned by APIs or functions and need to be checked:

let result = undefined;

if (!result) {
  console.log("Something went wrong.");
}

Using Boolean() to Test Values

You can use the Boolean() function to see if a value is truthy or falsy:

console.log(Boolean(0));        // false
console.log(Boolean(""));       // false
console.log(Boolean("hello"));  // true
console.log(Boolean([]));       // true

Summary Table

ValueTruthy/Falsy
falseFalsy
0Falsy
""Falsy
nullFalsy
undefinedFalsy
NaNFalsy
"0"Truthy
[]Truthy
{}Truthy
"hello"Truthy

Key Takeaways

  • JavaScript uses truthy and falsy values to evaluate conditions
  • Only 7 values are falsy (including false, 0, "", null, undefined, NaN, and -0)
  • Use Boolean() or test in an if to check truthiness