2 title: no-unneeded-ternary
12 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.
13 Here are some examples:
17 var isYes = answer === 1 ? true : false;
20 var isYes = answer === 1;
23 var isNo = answer === 1 ? false : true;
26 var isNo = answer !== 1;
29 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.
42 This rule disallow ternary operators when simpler alternatives exist.
44 Examples of **incorrect** code for this rule:
49 /*eslint no-unneeded-ternary: "error"*/
51 var a = x === 2 ? true : false;
53 var a = x ? true : false;
58 Examples of **correct** code for this rule:
63 /*eslint no-unneeded-ternary: "error"*/
65 var a = x === 2 ? "Yes" : "No";
69 var a = x ? "Yes" : "No";
73 f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below.
80 This rule has an object option:
82 * `"defaultAssignment": true` (default) allows the conditional expression as a default assignment pattern
83 * `"defaultAssignment": false` disallows the conditional expression as a default assignment pattern
87 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).
89 Examples of additional **incorrect** code for this rule with the `{ "defaultAssignment": false }` option:
94 /*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/
103 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).
105 ## When Not To Use It
107 You can turn this rule off if you are not concerned with unnecessary complexity in conditional expressions.