]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/eqeqeq.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / eqeqeq.md
CommitLineData
8f9d1d4d
DC
1---
2title: eqeqeq
8f9d1d4d
DC
3rule_type: suggestion
4---
5
6
eb39fafa
DC
7
8It is considered good practice to use the type-safe equality operators `===` and `!==` instead of their regular counterparts `==` and `!=`.
9
10The reason for this is that `==` and `!=` do type coercion which follows the rather obscure [Abstract Equality Comparison Algorithm](https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3).
11For instance, the following statements are all considered `true`:
12
13* `[] == false`
14* `[] == ![]`
15* `3 == "03"`
16
17If one of those occurs in an innocent-looking statement such as `a == b` the actual problem is very difficult to spot.
18
19## Rule Details
20
21This rule is aimed at eliminating the type-unsafe equality operators.
22
23Examples of **incorrect** code for this rule:
24
8f9d1d4d
DC
25::: incorrect
26
eb39fafa
DC
27```js
28/*eslint eqeqeq: "error"*/
29
30if (x == 42) { }
31
32if ("" == text) { }
33
34if (obj.getStuff() != undefined) { }
35```
36
8f9d1d4d
DC
37:::
38
eb39fafa
DC
39The `--fix` option on the command line automatically fixes some problems reported by this rule. A problem is only fixed if one of the operands is a `typeof` expression, or if both operands are literals with the same type.
40
41## Options
42
43### always
44
45The `"always"` option (default) enforces the use of `===` and `!==` in every situation (except when you opt-in to more specific handling of `null` [see below]).
46
47Examples of **incorrect** code for the `"always"` option:
48
8f9d1d4d
DC
49::: incorrect
50
eb39fafa
DC
51```js
52/*eslint eqeqeq: ["error", "always"]*/
53
54a == b
55foo == true
56bananas != 1
57value == undefined
58typeof foo == 'undefined'
59'hello' != 'world'
600 == 0
61true == true
62foo == null
63
64```
65
8f9d1d4d
DC
66:::
67
eb39fafa
DC
68Examples of **correct** code for the `"always"` option:
69
8f9d1d4d
DC
70::: correct
71
eb39fafa
DC
72```js
73/*eslint eqeqeq: ["error", "always"]*/
74
75a === b
76foo === true
77bananas !== 1
78value === undefined
79typeof foo === 'undefined'
80'hello' !== 'world'
810 === 0
82true === true
83foo === null
84
85```
86
8f9d1d4d
DC
87:::
88
eb39fafa
DC
89This rule optionally takes a second argument, which should be an object with the following supported properties:
90
91* `"null"`: Customize how this rule treats `null` literals. Possible values:
92 * `always` (default) - Always use === or !==.
93 * `never` - Never use === or !== with `null`.
94 * `ignore` - Do not apply this rule to `null`.
95
96### smart
97
98The `"smart"` option enforces the use of `===` and `!==` except for these cases:
99
100* Comparing two literal values
101* Evaluating the value of `typeof`
102* Comparing against `null`
103
104Examples of **incorrect** code for the `"smart"` option:
105
8f9d1d4d
DC
106::: incorrect
107
eb39fafa
DC
108```js
109/*eslint eqeqeq: ["error", "smart"]*/
110
111// comparing two variables requires ===
112a == b
113
114// only one side is a literal
115foo == true
116bananas != 1
117
118// comparing to undefined requires ===
119value == undefined
120```
121
8f9d1d4d
DC
122:::
123
eb39fafa
DC
124Examples of **correct** code for the `"smart"` option:
125
8f9d1d4d
DC
126::: correct
127
eb39fafa
DC
128```js
129/*eslint eqeqeq: ["error", "smart"]*/
130
131typeof foo == 'undefined'
132'hello' != 'world'
1330 == 0
134true == true
135foo == null
136```
137
8f9d1d4d
DC
138:::
139
eb39fafa
DC
140### allow-null
141
f2a92ac6 142**Deprecated:** Instead of using this option use `"always"` and pass a `"null"` option property with value `"ignore"`. This will tell ESLint to always enforce strict equality except when comparing with the `null` literal.
eb39fafa
DC
143
144```js
145["error", "always", {"null": "ignore"}]
146```
147
148## When Not To Use It
149
150If you don't want to enforce a style for using equality operators, then it's safe to disable this rule.