#TIL - Truthy vs. Falsy Values
These are two concepts that I felt like I knew but recently realized have been understanding very ‘vaguely.’ So let’s quickly do a TIL post on it.
Truthy
- Value that is considered
truein a boolean context. - Essentially, all values are truthy unless they are defined as falsy (what?)
- It’s actually easier to remember
falsyvalues since there are fewer of them. So let’s move on.
Falsy
- empty string,
"" false- duh0,-0,0nnullundefinedNaN
Previously during coding, I made the mistake of believing that an empty array [] or object {} was falsy just like an empty string "". So when my intended conditional if statement was not running in the way I expected, I was totally baffled. Well, that’s because I thought an empty {} was falsy!
Now that I think about it, it makes sense that {} and [] are truthy when "" is considered falsy, since the former are non-primitive values and the latter is primitive. So even though the inner contents are empty, the said array and object still points to a reference or point in memory.
Super quick TIL today, but something I’ve been meaning to document on my blog.