DEV Community

Gouthami
Gouthami

Posted on

Truthy and Falsy Values

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

Top comments (2)

Collapse
 
tracygjg profile image
Tracy Gilmore

In your list of falsy values, I would like to suggest the following.

  1. Using single quotes for an empty string is absolutely correct but might be a little confusing. Especially as an empty string can also be created as follows.
const emptyStringWithDoubleQuotes = "";
const emptyStringFromTemplateLiteral = ``;
Enter fullscreen mode Exit fullscreen mode
  1. To those unfamiliar with BigInt, example 7 might also be confusing. I think it might be worth adding (BigInt) after it as with empty string.
Collapse
 
gomigoku profile image
Gouthami

Absolutely! Thank you for pointing it out @tracygjg. I have surely overlooked this possibility.