]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow Null Comparisons (no-eq-null) |
2 | ||
3 | 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. | |
4 | ||
5 | ```js | |
6 | if (foo == null) { | |
7 | bar(); | |
8 | } | |
9 | ``` | |
10 | ||
11 | ## Rule Details | |
12 | ||
13 | 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 `!=`. | |
14 | ||
15 | Examples of **incorrect** code for this rule: | |
16 | ||
17 | ```js | |
18 | /*eslint no-eq-null: "error"*/ | |
19 | ||
20 | if (foo == null) { | |
21 | bar(); | |
22 | } | |
23 | ||
24 | while (qux != null) { | |
25 | baz(); | |
26 | } | |
27 | ``` | |
28 | ||
29 | Examples of **correct** code for this rule: | |
30 | ||
31 | ```js | |
32 | /*eslint no-eq-null: "error"*/ | |
33 | ||
34 | if (foo === null) { | |
35 | bar(); | |
36 | } | |
37 | ||
38 | while (qux !== null) { | |
39 | baz(); | |
40 | } | |
41 | ``` | |
609c276f TL |
42 | |
43 | ## Compatibility | |
44 | ||
45 | * **JSHint**: This rule corresponds to `eqnull` rule of JSHint. | |
46 | ||
47 | ## When Not To Use It | |
48 | ||
49 | If you want to enforce type-checking operations in general, use the more powerful [eqeqeq](./eqeqeq) instead. |