DEV Community

Discussion on: 10 JS Tips to Help You Code Smarter, Not Longer

Collapse
 
ukslim profile image
John Hartnup

The habit of using falsy checking has bitten me and my colleagues more than once.

if(cost) {
   showCost(cost)
} else {
   showCostInput()
}
Enter fullscreen mode Exit fullscreen mode

... intended to show an input field when cost is null/undefined ... also shows the input when cost is zero.

It's safer to use a smarter null-checker, like Ramda's isNil() or equivalents in Underscore etc. (Or trivially roll your own). And use it always, as a matter of habit, because it's quicker than thinking about it on a case-by-case basis.