unicorn/no-null

Disallow the null literal in favor of undefined

fixable

JavaScript has two values for "nothing". If you only use undefined, you need one check instead of two. Default parameters and destructuring defaults also apply to undefined but not to null.

Strict comparisons such as x === null are allowed by default, because you often need to check what an API returned.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/no-null" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/no-null" = ["error", {
  checkStrictEquality = false,
  checkArguments = true,
}]

Examples#

example.js1 problem
let selected = null;
example.jsNo problems
let selected;

if (document.querySelector("dialog") === null) {
  selected = undefined;
}

Options#

OptionDefaultWhat it does
checkStrictEqualityfalseAlso report x === null and x !== null.
checkArgumentstrueReport null that's passed to a function or a constructor.
example.jswith { "checkStrictEquality": true }1 problem
if (selected === null) {
  selected = first();
}
example.jswith { "checkArguments": false }No problems
save(user, null);

When not to use it#

Many APIs return or expect null, like the DOM, JSON and databases. If your code talks to them a lot, this rule gets in the way.