]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-unneeded-ternary | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | related_rules: | |
5 | - no-ternary | |
6 | - no-nested-ternary | |
7 | --- | |
8 | ||
9 | ||
eb39fafa DC |
10 | |
11 | It's a common mistake in JavaScript to use a conditional expression to select between two Boolean values instead of using ! to convert the test to a Boolean. | |
12 | Here are some examples: | |
13 | ||
14 | ```js | |
15 | // Bad | |
16 | var isYes = answer === 1 ? true : false; | |
17 | ||
18 | // Good | |
19 | var isYes = answer === 1; | |
20 | ||
eb39fafa DC |
21 | // Bad |
22 | var isNo = answer === 1 ? false : true; | |
23 | ||
24 | // Good | |
25 | var isNo = answer !== 1; | |
26 | ``` | |
27 | ||
28 | Another common mistake is using a single variable as both the conditional test and the consequent. In such cases, the logical `OR` can be used to provide the same functionality. | |
29 | Here is an example: | |
30 | ||
31 | ```js | |
32 | // Bad | |
33 | foo(bar ? bar : 1); | |
34 | ||
35 | // Good | |
36 | foo(bar || 1); | |
37 | ``` | |
38 | ||
39 | ## Rule Details | |
40 | ||
41 | This rule disallow ternary operators when simpler alternatives exist. | |
42 | ||
43 | Examples of **incorrect** code for this rule: | |
44 | ||
8f9d1d4d DC |
45 | ::: incorrect |
46 | ||
eb39fafa DC |
47 | ```js |
48 | /*eslint no-unneeded-ternary: "error"*/ | |
49 | ||
50 | var a = x === 2 ? true : false; | |
51 | ||
52 | var a = x ? true : false; | |
53 | ``` | |
54 | ||
8f9d1d4d DC |
55 | ::: |
56 | ||
eb39fafa DC |
57 | Examples of **correct** code for this rule: |
58 | ||
8f9d1d4d DC |
59 | ::: correct |
60 | ||
eb39fafa DC |
61 | ```js |
62 | /*eslint no-unneeded-ternary: "error"*/ | |
63 | ||
64 | var a = x === 2 ? "Yes" : "No"; | |
65 | ||
66 | var a = x !== false; | |
67 | ||
68 | var a = x ? "Yes" : "No"; | |
69 | ||
70 | var a = x ? y : x; | |
71 | ||
72 | f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below. | |
73 | ``` | |
74 | ||
8f9d1d4d DC |
75 | ::: |
76 | ||
eb39fafa DC |
77 | ## Options |
78 | ||
79 | This rule has an object option: | |
80 | ||
81 | * `"defaultAssignment": true` (default) allows the conditional expression as a default assignment pattern | |
82 | * `"defaultAssignment": false` disallows the conditional expression as a default assignment pattern | |
83 | ||
84 | ### defaultAssignment | |
85 | ||
86 | When set to `true`, which it is by default, The defaultAssignment option allows expressions of the form `x ? x : expr` (where `x` is any identifier and `expr` is any expression). | |
87 | ||
88 | Examples of additional **incorrect** code for this rule with the `{ "defaultAssignment": false }` option: | |
89 | ||
8f9d1d4d DC |
90 | ::: incorrect |
91 | ||
eb39fafa DC |
92 | ```js |
93 | /*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/ | |
94 | ||
95 | var a = x ? x : 1; | |
96 | ||
97 | f(x ? x : 1); | |
98 | ``` | |
99 | ||
8f9d1d4d DC |
100 | ::: |
101 | ||
eb39fafa DC |
102 | Note that `defaultAssignment: false` still allows expressions of the form `x ? expr : x` (where the identifier is on the right hand side of the ternary). |
103 | ||
104 | ## When Not To Use It | |
105 | ||
106 | You can turn this rule off if you are not concerned with unnecessary complexity in conditional expressions. |