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 nullbar !== undefined; // true if bar is anything other undefinedOr, one can conveniently test for both at the same time, using the loose equality operators (==/!=):
foo == null; // true if foo is null OR undefinedfoo == undefined; // same as previous line
foo != null; // true if foo is NEITHER null NOR undefinedfoo != undefined; // same as previous lineThis rule enforces that a consistent style is used for null/undefined checks.
Options
Section titled “Options”nullishComparisonStrictness
Section titled “nullishComparisonStrictness”This option has three possible values: "double-equals" (default), ignore, and triple-equals.
double-equals
Section titled “double-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:
if (x === null) { // code...}if (foo !== undefined) { // code...}if (x == null) { // code...}if (x != null) { // code...}"triple-equals"
Section titled “"triple-equals"”This option enforces that the strict equality operators are used for null/undefined checks.
Examples of code with the "triple-equals" option:
if (x == null) { // code...}if (foo != undefined) { // code...}if (x === null || x === undefined) { // code...}if (foo !== null && foo !== undefined) { // code...}if (bar === null) { // code...}"ignore"
Section titled “"ignore"”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) {}looseNullishComparisonStyle
Section titled “looseNullishComparisonStyle”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.
"prefer-null"
Section titled “"prefer-null"”This is the default simply because == null is 5 characters shorter than == undefined.
Examples of code with { looseNullishComparisonStyle: "prefer-null" }:
if (x == undefined) { // code...}if (foo != undefined) { // code...}if (x == null) { // code...}if (foo != null) { // code...}"prefer-undefined"
Section titled “"prefer-undefined"”Examples of code with { looseNullishComparisonStyle: "prefer-null" }:
if (x == null) { // code...}if (foo != null) { // code...}if (x == undefined) { // code...}if (foo != undefined) { // code...}"ignore"
Section titled “"ignore"”Examples of correct code with { looseNullishComparisonStyle: "ignore" }:
if (x == undefined) {}if (foo == null) {}When Not To Use It
Section titled “When Not To Use It”If you do not care about ensuring null/undefined checks are performed consistently in your codebase, you can safely disable this rule.
Related Rules
Section titled “Related Rules”- equalityOperators - Enforces consistent equality operator usage for scenarios other than null/undefined checks.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
no-eq-null - Oxlint:
eslint/no-eql-null