Skip to content

nullishCheckStyle

Enforces consistent equality operator usage when comparing with null or undefined.

✅ This rule is included in the ts preset.

When testing a value for being a nullish value (null or undefined), one can test for each nullish value separately using the strict equality operators (===/!==):

foo === null; // true if foo is null
bar !== undefined; // true if bar is anything other undefined

Or, one can conveniently test for both at the same time, using the loose equality operators (==/!=):

foo == null; // true if foo is null OR undefined
foo == undefined; // same as previous line
foo != null; // true if foo is NEITHER null NOR undefined
foo != undefined; // same as previous line

This rule enforces that a consistent style is used for null/undefined checks.

This option has three possible values: "double-equals" (default), ignore, and triple-equals.

This enforces that null/undefined checks are done using the loose equality operators, i.e. == null/== undefined.

"double-equals" is recommended since most code does not need to care about whether a nullish value is specifically null or specifically undefined. This mirrors the behavior of the built-in language features such as ?. and ??, which do not distinguish between nullish values. Testing for both values at once also means that null/undefined checks are less likely to be impacted when refactoring.

Examples of code with the "double-equals" option:

AE53 (AE=<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#FF6A83;--1:#A248">48nu53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AE53 (foAE!<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0undefin53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
443F53< (x spans="tyle--0:#C792EA;--1:#88">44348483F5<3) {divclass="Nameec-">de6376// cod3<..><}di vdivclass="Nameexpressive-">443F53< (x spans="tyle--0:#C792EA;--1:#88">44348483F5<3) {divclass="Nameec-">de6376// cod3<..>

This option enforces that the strict equality operators are used for null/undefined checks.

Examples of code with the "triple-equals" option:

AE53 (AE<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#FF6A83;--1:#A248">48nu53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AE53 (foAE5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0undefin53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
443F53< (x spans="tyle--0:#C792EA;--1:#88">44A348483443F5<3 x spans="tyle--0:#C792EA;--1:#88">44A361B0undef3F5<3) {divclass="Nameec-">de6376// cod3<..><}di vdivclass="Nameexpressive-">443F53 (44A348483443F53 44A361B0undef3F5<3) {divclass="Nameec-">de6376// cod3<..><}di vdivclass="Nameexpressive-">443F53 (44A348483F5<3) {divclass="Nameec-">de6376// cod3<..>

This option does not enforce any preference between == and === in null/undefined checks.

Examples of valid code with this option:

if (x == null) {
}
if (foo === null) {
}

Type: "prefer-null" | "prefer-undefined"| "ignore" Default: "prefer-null"

Since x == null and x == undefined are equivalent, this option allows enforcing that the same choice is used consistently.

This is the default simply because == null is 5 characters shorter than == undefined.

Examples of code with { looseNullishComparisonStyle: "prefer-null" }:

AE53 (AE<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0undefin53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AE53 (foAE5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0undefin53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
443F53< (x spans="tyle--0:#C792EA;--1:#88">44348483F5<3) {divclass="Nameec-">de6376// cod3<..><}di vdivclass="Nameexpressive-">443F53 (44348483F5<3) {divclass="Nameec-">de6376// cod3<..>

Examples of code with { looseNullishComparisonStyle: "prefer-null" }:

AE53 (AE<==sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#FF6A83;--1:#A248">48nu53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>AE53 (foAE5<3 sp ansty="le--0:#FF6A83;--1:#A248">48nu53)< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// code.<..d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
443F53< (x spans="tyle--0:#C792EA;--1:#88">44361B0undef3F5<3) {divclass="Nameec-">de6376// cod3<..><}di vdivclass="Nameexpressive-">443F53 (44361B0undef3F5<3) {divclass="Nameec-">de6376// cod3<..>

Examples of correct code with { looseNullishComparisonStyle: "ignore" }:

if (x == undefined) {
}
if (foo == null) {
}

If you do not care about ensuring null/undefined checks are performed consistently in your codebase, you can safely disable this rule.

  • equalityOperators - Enforces consistent equality operator usage for scenarios other than null/undefined checks.