1 # Require === and !== (eqeqeq)
3 It is considered good practice to use the type-safe equality operators `===` and `!==` instead of their regular counterparts `==` and `!=`.
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`:
12 If one of those occurs in an innocent-looking statement such as `a == b` the actual problem is very difficult to spot.
16 This rule is aimed at eliminating the type-unsafe equality operators.
18 Examples of **incorrect** code for this rule:
21 /*eslint eqeqeq: "error"*/
27 if (obj.getStuff() != undefined) { }
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.
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]).
38 Examples of **incorrect** code for the `"always"` option:
41 /*eslint eqeqeq: ["error", "always"]*/
47 typeof foo == 'undefined'
55 Examples of **correct** code for the `"always"` option:
58 /*eslint eqeqeq: ["error", "always"]*/
64 typeof foo === 'undefined'
72 This rule optionally takes a second argument, which should be an object with the following supported properties:
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`.
81 The `"smart"` option enforces the use of `===` and `!==` except for these cases:
83 * Comparing two literal values
84 * Evaluating the value of `typeof`
85 * Comparing against `null`
87 Examples of **incorrect** code for the `"smart"` option:
90 /*eslint eqeqeq: ["error", "smart"]*/
92 // comparing two variables requires ===
95 // only one side is a literal
99 // comparing to undefined requires ===
103 Examples of **correct** code for the `"smart"` option:
106 /*eslint eqeqeq: ["error", "smart"]*/
108 typeof foo == 'undefined'
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.
120 ["error", "always", {"null": "ignore"}]
123 ## When Not To Use It
125 If you don't want to enforce a style for using equality operators, then it's safe to disable this rule.