Is there an elegant way to check if variable is NOT falsy but in case of 0 it passes. The issue with this way of verifying
if(var !== undefined && var !== null)
is that it’s long and doesn’t cover all cases like
NaN. I’m also using typescript and declare it as optional number.
You can do exactly what your first sentence asks:
if (!x && x !== 0)
means literally “if x is falsy and x is not 0”.
!= comparison operators explicitly consider
undefined to be equal, so
if (x != null)
true for both