One thing I like about JavaScript is its shothand evaluation whether a variable contains a value. Instead of the tedious if (text !== undefined &am...
For further actions, you may consider blocking this person and/or reporting abuse
I love this feature of JavaScript, but there are some gotchas developers should be aware of:
1) Empty arrays are truthy, one would easily have thought otherwise
2)
NaNis falsy. Instead of trying to check if a number isNaN, check if a number is falsy. this could be very handy in certain circumstances, except for that3) The number
0is falsy. Yep thats right, so if you use anif (number) { ...}to safeguard againstNaN, you won't perform any computations on 0 as well.In JSX, there is also an issue with conditions not being boolean, say in usages like this:
If, somehow,
someVariableis NaN here, the letters "NaN" would show up on the screen! I'm surpriced how often I've actually spotted "bugs" like this in applications I have maintained. Often they have gone unnoticed because it's been barely visible, or just been a temporary state. In cases like these, you can use!!to enforcesomeVariableto become a boolean as well, and React won't render booleans to the screen:Thank you for pointing out those exceptions. I got caught by
0being falsy couple of times as well.And btw my personal favorite among falsy JS values is
document.all, although one probably won't use it so often in the actual code.