DEV Community

Discussion on: Null-checking in JavaScript

Collapse
 
joelnet profile image
JavaScript Joel • Edited

Looking at this block of code...

if (
  tokenInfo &&
  tokenInfo !== undefined &&   
  tokenInfo !== null &&
  tokenInfo !== ""
  ) { }

If the first check...

if (tokenInfo) {
//  ---------
//            \
//             first check

... is undefined, null, or "", or any other falsy valuue... then the if will short circuit and not perform any of the other checks. This means it is impossible for the tokenInfo !== undefined check (and others) to ever evaluate to true.

So that block of code is identical to this block:

if (tokenInfo) { }

The other checks are unnecessary as they won't change the function at all.