Truthy and Falsy Values in JavaScript

Longtime JavaScript developers often toss around the terms "truthy" and "falsy", but for those who are newer to JavaScript these terms can be a bit mystifying.

When we say that a value is "truthy" in JavaScript, we don't just mean that the value is true. Rather, what we mean is that the value coerces to true when evaluated in a boolean context. Let's look at what that means.

As you can see, there are a lot of truthy values in JavaScript. And there are many more than could be listed here. On the other side, though, there are only six falsy values. In fact, because the list of falsy values is so short, memorizing that list is the easiest way to tell whether a value is truthy or falsy. Here is the list:

Taking advantage of truthiness can make your code a little bit more concise. We don't need to explicitly check for undefined, "", etc. Instead we can just check whether person.skepticism is truthy. However, there some caveats to keep in mind.

Firstly, this approach only works if all of the falsy values should be excluded (or included.) For instance, if a value of 0 or "" was meaningful, then the if above would not function correctly.

Secondly, it is important to keep in mind that truthiness is not the same as == true. The algorithm for loose equality is much more complicated than for truthiness. We'll go into more detail about that in a future drip.

I hope this introduction to truthiness and falsiness helps you to write clearer, more concise JavaScript.

Thanks for reading!

Josh Clanton

Want to improve your JavaScript skills?
Subscribe to A Drip of JavaScript for biweekly tips to help you level up as a JS developer.