]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: eqeqeq | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
5 | ||
6 | ||
eb39fafa DC |
7 | |
8 | It is considered good practice to use the type-safe equality operators `===` and `!==` instead of their regular counterparts `==` and `!=`. | |
9 | ||
10 | The 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). | |
11 | For instance, the following statements are all considered `true`: | |
12 | ||
13 | * `[] == false` | |
14 | * `[] == ![]` | |
15 | * `3 == "03"` | |
16 | ||
17 | If 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 | ||
21 | This rule is aimed at eliminating the type-unsafe equality operators. | |
22 | ||
23 | Examples of **incorrect** code for this rule: | |
24 | ||
8f9d1d4d DC |
25 | ::: incorrect |
26 | ||
eb39fafa DC |
27 | ```js |
28 | /*eslint eqeqeq: "error"*/ | |
29 | ||
30 | if (x == 42) { } | |
31 | ||
32 | if ("" == text) { } | |
33 | ||
34 | if (obj.getStuff() != undefined) { } | |
35 | ``` | |
36 | ||
8f9d1d4d DC |
37 | ::: |
38 | ||
eb39fafa DC |
39 | The `--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 | ||
45 | The `"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 | ||
47 | Examples of **incorrect** code for the `"always"` option: | |
48 | ||
8f9d1d4d DC |
49 | ::: incorrect |
50 | ||
eb39fafa DC |
51 | ```js |
52 | /*eslint eqeqeq: ["error", "always"]*/ | |
53 | ||
54 | a == b | |
55 | foo == true | |
56 | bananas != 1 | |
57 | value == undefined | |
58 | typeof foo == 'undefined' | |
59 | 'hello' != 'world' | |
60 | 0 == 0 | |
61 | true == true | |
62 | foo == null | |
63 | ||
64 | ``` | |
65 | ||
8f9d1d4d DC |
66 | ::: |
67 | ||
eb39fafa DC |
68 | Examples of **correct** code for the `"always"` option: |
69 | ||
8f9d1d4d DC |
70 | ::: correct |
71 | ||
eb39fafa DC |
72 | ```js |
73 | /*eslint eqeqeq: ["error", "always"]*/ | |
74 | ||
75 | a === b | |
76 | foo === true | |
77 | bananas !== 1 | |
78 | value === undefined | |
79 | typeof foo === 'undefined' | |
80 | 'hello' !== 'world' | |
81 | 0 === 0 | |
82 | true === true | |
83 | foo === null | |
84 | ||
85 | ``` | |
86 | ||
8f9d1d4d DC |
87 | ::: |
88 | ||
eb39fafa DC |
89 | This 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 | ||
98 | The `"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 | ||
104 | Examples 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 === | |
112 | a == b | |
113 | ||
114 | // only one side is a literal | |
115 | foo == true | |
116 | bananas != 1 | |
117 | ||
118 | // comparing to undefined requires === | |
119 | value == undefined | |
120 | ``` | |
121 | ||
8f9d1d4d DC |
122 | ::: |
123 | ||
eb39fafa DC |
124 | Examples of **correct** code for the `"smart"` option: |
125 | ||
8f9d1d4d DC |
126 | ::: correct |
127 | ||
eb39fafa DC |
128 | ```js |
129 | /*eslint eqeqeq: ["error", "smart"]*/ | |
130 | ||
131 | typeof foo == 'undefined' | |
132 | 'hello' != 'world' | |
133 | 0 == 0 | |
134 | true == true | |
135 | foo == 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 | ||
150 | If you don't want to enforce a style for using equality operators, then it's safe to disable this rule. |