question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-null should allow, or have an option to allow, the `null == foo` pattern for value-presence-checking

See original GitHub issue

A common pattern for checking for both undefined and null in a single operation is to intentionally use the coercive equality operator with null as the comparee:

if (foo == null)
   // ...

At the moment, this triggers the otherwise-wonderful no-null lint.

In the codebases I’ve worked on, this specific pattern is the only place that either:

  1. coerceive equality, or
  2. null

… is allowed. At least as far as I’m aware, that’s a common situation (those two being allowed in this specific pattern.)

Personally, despite the modern world with linting and TypeScript, I also still prefer yoda-comparisons; so I’d appreciate if this also allows (or the option also allows, if we add an option for this) the reversed version:

if (null == foo)
   // ...

I hope this Issue was helpful, and not just a waste of everyone’s time! 😅

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
ELLIOTTCABLEcommented, Aug 31, 2021
  1. “Where would you want it” - you can excise null from your own codebase (great!), but you should still be checking for it at API entrypoints. Code defensively!

  2. “Better to test the two separately” - Well, yes, maybe, although that’s a matter of taste — two checks has a vanishingly minimal perf impact, of course; but mostly I’m of the camp that thinks that’s way, way too much text for a feature that’s explicitly built into the language (i.e. the explicit “checking for null and undefined, but no other values” expressed by that loose-comparison.)

    Saying ‘don’t use null == value because it’s less explicit’ feels a bit like saying ‘don’t use foo?.bar because if (typeof foo !== 'undefined' && foo !== null) { foo.bar } is more explicit’. That’s why we have syntactic sugar for common patterns! 😛

I won’t be offended if this isn’t to y’all’s tastes and you decide to close this, though. ¯\_(ツ)_/¯

1reaction
sindresorhuscommented, Sep 19, 2021

Can you not use if (foo != undefined) {} instead? It also matches null.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sindresorhus/eslint-plugin-unicorn (Raised $2856.00)
no-null should allow, or have an option to allow, the `null == foo` pattern for value-presence-checking. Unfunded#1507created byELLIOTTCABLE.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found