Zach Snoek
Zach Snoek's Blog

Zach Snoek's Blog

What are truthy and falsy in JavaScript?

Zach Snoek's photo
Zach Snoek
·Dec 11, 2020·

2 min read

If you write JavaScript, you've probably heard values called truthy and falsy before. But what do truthy and falsy mean and what values are considered each?

Definitions

Truthy values are values that can be coerced to true in a Boolean context, like an if statement. Falsy values are considered false in a Boolean context. That means that a value is truthy if it causes an if block or while loop to execute, for example.

Truthy and Falsy Values

Falsy

There are eight falsy values in JavaScript: false, 0, -0, 0n, "", null, undefined, and NaN.

Truthy

Truthy values are a little easier to remember: all values that are not falsy are truthy.

Here are some examples of truthy values: true, 42, -42, {}, "false", "0", and new Date().

If you're ever curious if a value is truthy or falsy, here's a short and simple function to help you out:

const isTruthyOrFalsy = (value)  => value ? 'truthy' : 'falsy'

Conclusion

Simply put, truthy values are true in a Boolean context and falsy values are false in a Boolean context. Hopefully this post helps you understand JavaScript values a little better.

References and Further Reading


Let's connect

Thanks for reading! If you found this post helpful, come connect with me on Twitter, LinkedIn, and GitHub!

 
Share this