|
| 1 | +<!--- |
| 2 | +{ |
| 3 | + "titles": [ |
| 4 | + "eqeqeq" |
| 5 | + ], |
| 6 | + "slugs": [ |
| 7 | + "option-jshint-eqeqeq" |
| 8 | + ], |
| 9 | + "linters": [ |
| 10 | + "jshint" |
| 11 | + ], |
| 12 | + "author": "jallardice", |
| 13 | + "subject": "option" |
| 14 | +} |
| 15 | +--> |
| 16 | + |
| 17 | +### What does this option do? |
| 18 | + |
| 19 | +The JSHint `eqeqeq` option is used to prohibit the use of the equals operator |
| 20 | +`==` and the does-not-equal operator `!=`. This enforces the use of the strict |
| 21 | +equality operators (`===` and `!==` instead). The strict equality operators |
| 22 | +differ from their non-strict counterparts by first comparing the type of each |
| 23 | +operand, rather than attempting to coerce them to a common type. In the |
| 24 | +following example we make use of the non-strict equality operator to check |
| 25 | +whether a value is either `null` or `undefined`: |
| 26 | + |
| 27 | +<!--- |
| 28 | +{ |
| 29 | + "linter": "jshint" |
| 30 | +} |
| 31 | +--> |
| 32 | +```javascript |
| 33 | +/*jshint eqeqeq: true */ |
| 34 | +var x; |
| 35 | +if (x == null) { |
| 36 | + // This will execute if x is null or undefined |
| 37 | + doSomething(); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### When should I use this option? |
| 42 | + |
| 43 | +The use of the `eqeqeq` JSHint option will cause an "Expected '===' and instead |
| 44 | +saw '=='" error any time it encounters an equals or does-not-equal operator. As |
| 45 | +demonstrated in the above example these operators can be used in some situations |
| 46 | +to produce shorter code. However, their use can lead to bugs and unexpected |
| 47 | +behaviour. For that reason it's generally considered best practice to use the |
| 48 | +strict equality operators wherever possible. The example above can be written as |
| 49 | +follows instead: |
| 50 | + |
| 51 | +<!--- |
| 52 | +{ |
| 53 | + "linter": "jshint" |
| 54 | +} |
| 55 | +--> |
| 56 | +```javascript |
| 57 | +/*jshint eqeqeq: true */ |
| 58 | +var x; |
| 59 | +if (typeof x === "undefined" || x === null) { |
| 60 | + // This will execute if x is null or undefined |
| 61 | + doSomething(); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +Note that this is an *enforcing* option which means JSHint does not apply it by |
| 66 | +default. If you do not explicitly set this option to `true` JSHint will allow |
| 67 | +the use of non-strict equality operators anywhere in your code. |
| 68 | + |
| 69 | +#### Recommendation |
| 70 | + |
| 71 | +Set this option to `true` (enforces the use of strict equality operators). |
0 commit comments