]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-eq-null.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-eq-null.md
CommitLineData
eb39fafa
DC
1# Disallow Null Comparisons (no-eq-null)
2
3Comparing 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
6if (foo == null) {
7 bar();
8}
9```
10
11## Rule Details
12
13The `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
15Examples of **incorrect** code for this rule:
16
17```js
18/*eslint no-eq-null: "error"*/
19
20if (foo == null) {
21 bar();
22}
23
24while (qux != null) {
25 baz();
26}
27```
28
29Examples of **correct** code for this rule:
30
31```js
32/*eslint no-eq-null: "error"*/
33
34if (foo === null) {
35 bar();
36}
37
38while (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
49If you want to enforce type-checking operations in general, use the more powerful [eqeqeq](./eqeqeq) instead.