Course contentsShow
← JavaScript
Lesson 203 of 2,7236. Booleans and ConditionalsFree lesson

Common Truthy/Falsy Pitfalls

Avoiding mistakes with empty arrays, empty objects, and the string '0' which are all truthy.

Common Truthy/Falsy Pitfalls

You'll learn the surprising truthy values that trip up beginners: empty arrays, empty objects, and the string '0'.

The Unexpected Truthy Trio

You've learned that JavaScript has specific falsy values (false, 0, "", null, undefined, NaN). Everything else is truthy. But some values look or feel like they should be falsy when they're actually truthy, causing bugs.

Empty Arrays Are Truthy

An empty array [] has no elements, but it's still an object in JavaScript—and all objects are truthy:

if ([]) {
  console.log("This runs!"); // Surprise: empty array is truthy
}

Boolean([]);  // true

Think of it like this: An empty box still exists as a box. It's not nothing—it's something (an object) that happens to contain nothing.

Empty Objects Are Truthy

Same principle applies to empty objects {}:

if ({}) {
  console.log("This also runs!"); // Empty object is truthy
}

Boolean({});  // true

The String '0' Is Truthy

The number 0 is falsy, but the string "0" is truthy—it's a non-empty string:

if ("0") {
  console.log("String '0' is truthy!"); // This runs
}

Boolean("0");   // true
Boolean(0);     // false (the number zero is falsy)

Why This Matters

These pitfalls cause errors when you expect something to be falsy but it's not. If you need to check for truly empty arrays or objects, you must check their properties explicitly (concepts you'll learn later), not rely on boolean conversion.

Key Takeaway: Empty arrays [], empty objects {}, and the string "0" are all truthy—only the specific falsy values (empty string "", number 0, false, null, undefined, NaN) are falsy.