7 ms·
Another quick option for avoiding the "zero" issue: {!!number && <Thing />}
by redler 5y ago
Another quick option for avoiding the "zero" issue:
{!!number && <Thing />}
- 9wzYQbTYsAIc 5y agoOne of the joys of JavaScript, right there: not-not sorry.
- dbetteridge 5y agoBetter to use Boolean(number) Its more obvious what it achieves just at a glance.
- postalrat 5y ago"!!" should be pretty obvious to any javascript developer.
- 9wzYQbTYsAIc 5y agoNonetheless, better for the industry if we start to embrace more uniformity in our idioms. You can practically copy-and-paste between JavaScript and C# these days, with some trivial text replacement tweaks, if you are careful with your idioms.
- postalrat 5y agoWhat if you wanted to get the boolean value of number but with not. Would you write: !Boolean(n) Or would you write: !n
- 9wzYQbTYsAIc 5y agoif it were C#, I’d write it along the lines of !(n as bool), but for the purposes of JS, I suppose something like: const IsNumber = (value) => Boolean(value); !IsNumber(n) I’m not a fan of using the return-type as the function name, especially when you are really just trying to find out if something is a number.
- dbetteridge 5y agoIt might be, but not everyone knows js and I don't expect future people to be me. More obvious and more readable is always better