]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/eqeqeq.md
4849f999967fd7f2c4ae0645c5cd37848d6c7247
[pve-eslint.git] / eslint / docs / rules / eqeqeq.md
1 # Require === and !== (eqeqeq)
2
3 It is considered good practice to use the type-safe equality operators `===` and `!==` instead of their regular counterparts `==` and `!=`.
4
5 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).
6 For instance, the following statements are all considered `true`:
7
8 * `[] == false`
9 * `[] == ![]`
10 * `3 == "03"`
11
12 If one of those occurs in an innocent-looking statement such as `a == b` the actual problem is very difficult to spot.
13
14 ## Rule Details
15
16 This rule is aimed at eliminating the type-unsafe equality operators.
17
18 Examples of **incorrect** code for this rule:
19
20 ```js
21 /*eslint eqeqeq: "error"*/
22
23 if (x == 42) { }
24
25 if ("" == text) { }
26
27 if (obj.getStuff() != undefined) { }
28 ```
29
30 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.
31
32 ## Options
33
34 ### always
35
36 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]).
37
38 Examples of **incorrect** code for the `"always"` option:
39
40 ```js
41 /*eslint eqeqeq: ["error", "always"]*/
42
43 a == b
44 foo == true
45 bananas != 1
46 value == undefined
47 typeof foo == 'undefined'
48 'hello' != 'world'
49 0 == 0
50 true == true
51 foo == null
52
53 ```
54
55 Examples of **correct** code for the `"always"` option:
56
57 ```js
58 /*eslint eqeqeq: ["error", "always"]*/
59
60 a === b
61 foo === true
62 bananas !== 1
63 value === undefined
64 typeof foo === 'undefined'
65 'hello' !== 'world'
66 0 === 0
67 true === true
68 foo === null
69
70 ```
71
72 This rule optionally takes a second argument, which should be an object with the following supported properties:
73
74 * `"null"`: Customize how this rule treats `null` literals. Possible values:
75 * `always` (default) - Always use === or !==.
76 * `never` - Never use === or !== with `null`.
77 * `ignore` - Do not apply this rule to `null`.
78
79 ### smart
80
81 The `"smart"` option enforces the use of `===` and `!==` except for these cases:
82
83 * Comparing two literal values
84 * Evaluating the value of `typeof`
85 * Comparing against `null`
86
87 Examples of **incorrect** code for the `"smart"` option:
88
89 ```js
90 /*eslint eqeqeq: ["error", "smart"]*/
91
92 // comparing two variables requires ===
93 a == b
94
95 // only one side is a literal
96 foo == true
97 bananas != 1
98
99 // comparing to undefined requires ===
100 value == undefined
101 ```
102
103 Examples of **correct** code for the `"smart"` option:
104
105 ```js
106 /*eslint eqeqeq: ["error", "smart"]*/
107
108 typeof foo == 'undefined'
109 'hello' != 'world'
110 0 == 0
111 true == true
112 foo == null
113 ```
114
115 ### allow-null
116
117 **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.
118
119 ```js
120 ["error", "always", {"null": "ignore"}]
121 ```
122
123 ## When Not To Use It
124
125 If you don't want to enforce a style for using equality operators, then it's safe to disable this rule.