Use == null Comparison

The == null check is a concise expression to identify nullish values (null and undefined).

This refactoring replaces the following combinations of longer strict equality checks with the shorter null comparison:

  • a === null || a === undefined becomes a == null
  • b !== null && b !== undefined becomes b != null
  • x.f(1, 2) === null || x.f(1, 2) === undefined becomes x.f(1, 2) == null

Learn More: Equality comparisons and sameness (MDN), Equality table

When two similar-looking function calls have a side effect, this refactoring can change the behavior of the code.

For example, the refactoring changes:

let a = f(1) === null || f(1) === undefined;

into

let a = f(1) == null;

If f(1) has a side effect, it would have been called once or twice before the refactoring, and once after the refactoring. This means that the side effect would have been called a different number of times, potentially changing the behavior.

Original Code

Changes

1const x1 = a === null || a === undefined;1const x1 = a == null;
2const x2 = b !== null && b !== undefined;2const x2 = b != null;
3const x3 = f(1, 2) === null || f(1, 2) === undefined;3const x3 = f(1, 2) == null;
1const x1 = a === null || a === undefined;
2const x2 = b !== null && b !== undefined;
3const x3 = f(1, 2) === null || f(1, 2) === undefined;
1const x1 = a == null;
2const x2 = b != null;
3const x3 = f(1, 2) == null;

Transformed Code

const x1 = a == null;
const x2 = b != null;
const x3 = f(1, 2) == null;

Explore More