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:
- 0
- -0
- '' (empty string)
- undefined
- null
- NaN
- 0n
- false
- document.all
Anything other than these are truthy values. For example, empty array, empty object etc.
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:
Top comments (2)
In your list of falsy values, I would like to suggest the following.
Absolutely! Thank you for pointing it out @tracygjg. I have surely overlooked this possibility.