What are falsy values in JavaScript?

The falsy values in JavaScript are 0, 0n, null, undefined, false, NaN, and the empty string "". They evaluate to false when coerced by JavaScript’s typing engine into a boolean value, but they are not necessarily equal to each other.

Image for post
Image for post

Falsy values in JavaScript

“A falsy value is a value that is considered false when encountered in a Boolean context.” — Mozilla Developer Network

In JavaScript, there is a specialist list of following 7 values, which are called falsy values — they all evaluate to false in conditionals:

  • the number 0
  • the BigInt 0n
  • the keyword null
  • the keyword undefined
  • the boolean false
  • the number NaN
  • the empty string "" (equivalent to '' or ``)

Of course, learning about falsy values begs the question, what are truthy values in JavaScript?

Truthy values are going to be everything else — anything that is not falsy evaluates to true in a conditional.

Truthy values include the empty object {} and the empty array [] — since they aren’t falsy, they are truthy, by definition.

Code example of falsy values

Here is a complete list of falsy comparisons using the loose equality (==) double equals comparison operator:

Despite all being falsy, they are not all equal with the double equals ==:

  1. The values null and undefined are loosely equal to each other.
  2. NaN is not equal to any other value, not even itself.
  3. The other falsy values (0, 0n, false, and "") are all loosely equal.

A note on strict equality

All the above falsy values would be strictly equal to only themselves using ===, with the exception of NaN, which is the only value not strictly equal to itself in JavaScript.

Here is a complete list of falsy comparisons using the strict equality (===) triple equals comparison operator:

Image for post
Image for post

Conclusion

Falsy values are the way that JavaScript deals with not crashing when a conditional is passed something other than a boolean value.

Other programming languages differ, but JavaScript has its certain falsy values, and everything else is going to evaluate to true in a conditional.

The 7 falsy values are: 0, 0n, null, undefined, false, NaN, and "".

The falsy values sort-of loosely equal each other with ==, at least in that 0==0n==false=="", null==undefined, and of course remembering that NaN does not equal anything, not even itself.

Now that you understand falsy statements, try them out inside of conditionals (if statements, question mark ? conditionals, and loops) in JavaScript.

Coding at Dawn

Laid-back programming + relaxing photography

Dr. Derek Austin 🥳

Written by

🤓 The physical therapist who writes JavaScript 💪 Full-Stack Web Developer 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 🗯️ Ask me anything 👉 DoctorDerek.com 👈

Coding at Dawn

Laid-back programming + relaxing photography

Dr. Derek Austin 🥳

Written by

🤓 The physical therapist who writes JavaScript 💪 Full-Stack Web Developer 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 🗯️ Ask me anything 👉 DoctorDerek.com 👈

Coding at Dawn

Laid-back programming + relaxing photography

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store