Disallow null
comparisons without type-checking operators
Comparing to null
without a type-checking operator (==
or !=
), can have unintended results as the comparison will evaluate to true when comparing to not just a null
, but also an undefined
value.
if (foo == null) {
bar();
}
The no-eq-null
rule aims reduce potential bug and unwanted behavior by ensuring that comparisons to null
only match null
, and not also undefined
. As such it will flag comparisons to null when using ==
and !=
.
Examples of incorrect code for this rule:
/*eslint no-eq-null: "error"*/
if (foo == null) {
bar();
}
while (qux != null) {
baz();
}
Examples of correct code for this rule:
/*eslint no-eq-null: "error"*/
if (foo === null) {
bar();
}
while (qux !== null) {
baz();
}
If you want to enforce type-checking operations in general, use the more powerful eqeqeq instead.
eqnull
rule of JSHint.This rule was introduced in ESLint v0.0.9.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-eq-null