DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in

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.