Truthy and Falsy Values

Gouthami - May 19 - - Dev Community

Yes! JavaScript is weird, interesting and very dynamic too. It has so many things that you will get tired of learning, if you are thinking of mastering everything at one go.

Today I am going to talk about something that I learned when I came across this topic. I might not go too depth on this, but I will tell you what you must know about these things.

So we have Boolean true or false values right? then why do we have this truthy and falsy values again to make a block of code to run or not to run and when exactly then come into picture?

Sometimes what happens you know, you have written some expression which ultimately evaluates to boolean value that is either true or false in Boolean context, these values are called as truthy and falsy values.

You don't have to particularly remember about the truthy values, just make of note of falsy values, rest all of them are considered as truthy values.

Falsy values:

  1. 0
  2. -0
  3. '' (empty string)
  4. undefined
  5. null
  6. NaN
  7. 0n
  8. false
  9. document.all

Falsy Values

Anything other than these are truthy values. For example, empty array, empty object etc.

Truthy Values

So now you know how to check if a expression evaluates to truthy or falsy value so that the you can put a proper condition to manage your code.

For example:

Truthy and falsy values in expression

.