A Developer’s Guide to Truthy and Falsy in TypeScript

As a TypeScript developer, you’ve almost certainly written code like if (myVariable) { ... } to check if a variable “exists” or has a “valid” value. But what’s really happening under the hood? This check isn’t just for null or undefined; it’s a core concept in JavaScript and TypeScript called “truthiness”.

Understanding the difference between truthy and falsy values is crucial for writing robust, bug-free code. It helps you avoid common pitfalls and write more concise, expressive logic. Let’s dive in!

What are Truthy and Falsy Values?

In TypeScript, every value has an inherent boolean quality. When used in a boolean context (like an if condition), a value will be coerced, or converted, into either true or false.

  • falsy value is a value that is considered false when encountered in a boolean context.
  • truthy value is any value that is considered true in a boolean context. Basically, if it’s not on the falsy list, it’s truthy!

It’s much easier to just memorize the short list of falsy values.

The Official List of Falsy Values

There are only a handful of falsy values in TypeScript/JavaScript. Everything else is truthy.

The Falsy List: false, 0, -0, 0n (BigInt zero), “” (empty string), null, undefined, and NaN.

Anytime one of these values is used where a boolean is expected, it will behave like false.

Truthy and Falsy in Action

The most common place you’ll see this implicit coercion is in conditional statements like ifwhile, and the ternary operator.

Let’s create a small helper function to see this clearly:

function checkTruthiness(value: any) {
  console.log(Testing value: ${JSON.stringify(value)});
  if (value) {
    console.log(" This is TRUTHY!");
  } else {
    console.log(" This is FALSY!");
  }
  console.log("---");
}

// --- Falsy Examples ---
checkTruthiness(false);
checkTruthiness(0);
checkTruthiness("");
checkTruthiness(null);
checkTruthiness(undefined);
checkTruthiness(NaN);

// --- Truthy Examples ---
checkTruthiness(true);
checkTruthiness(10);
checkTruthiness("hello");
checkTruthiness({}); // An empty object is truthy!
checkTruthiness([]); // An empty array is truthy!

Common Gotchas: Empty Objects and Arrays

Pay close attention to the last two examples. This is a classic trip-up for developers!

  • An empty object {} is truthy.
  • An empty array [] is truthy.

If you need to check if an array has items, you can’t just do if (myArray). You must explicitly check its length: if (myArray.length > 0).

Explicit Coercion with the Boolean() Function

While if statements perform implicit coercion, you can also explicitly convert any value to its boolean equivalent using the Boolean() function. This is a great way to test your understanding and can be very useful in its own right.

The Boolean() function will return true for any truthy value and false for any falsy value.

Let’s see a comparison:

ValueExpressionResult
"" (empty string)Boolean("")false
"Ankur"Boolean("Ankur")true
0Boolean(0)false
42Boolean(42)true
nullBoolean(null)false
[] (empty array)Boolean([])true
{} (empty object)Boolean({})true
undefinedBoolean(undefined)false

Practical Use Case: Filtering an Array

One of the most elegant uses of this concept is cleaning up arrays. Imagine you have an array with mixed values, and you want to remove all the falsy ones (like null0, and undefined).

Instead of a complicated loop, you can pass the Boolean function directly to Array.prototype.filter.

const mixedValues = [0, 1, "hello", "", null, "world", undefined, [], {}];

// The filter method calls Boolean() on each item.
// It keeps only the items where Boolean(item) returns true.
const truthyValues = mixedValues.filter(Boolean);

console.log(truthyValues);
// Output: [1, "hello", "world", [], {}]

Isn’t that clean? The filter(Boolean) pattern is a fantastic trick to have in your developer toolkit. It’s concise, readable, and perfectly demonstrates the power of truthy/falsy checks.

Conclusion

Mastering the concept of truthy and falsy is a small step that pays huge dividends in the quality of your TypeScript code.

Here’s the key takeaway:

  1. Memorize the short list of falsy values: false0-00n""nullundefined, and NaN.
  2. Remember that everything else, including empty objects {} and empty arrays [], is truthy.
  3. Leverage this knowledge to write cleaner conditionals and use powerful shortcuts like array.filter(Boolean).

By keeping these simple rules in mind, you’ll write more predictable and robust code while avoiding some of the most common bugs in the JavaScript ecosystem.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.