2 title: no-extra-boolean-cast
10 In contexts such as an `if` statement's test where the result of the expression will already be coerced to a Boolean, casting to a Boolean via double negation (`!!`) or a `Boolean` call is unnecessary. For example, these `if` statements are equivalent:
28 This rule disallows unnecessary boolean casts.
30 Examples of **incorrect** code for this rule:
35 /*eslint no-extra-boolean-cast: "error"*/
39 var foo = !!bar ? baz : bat;
41 var foo = Boolean(!!bar);
43 var foo = new Boolean(!!bar);
59 } while (Boolean(foo));
68 Examples of **correct** code for this rule:
73 /*eslint no-extra-boolean-cast: "error"*/
76 var foo = Boolean(bar);
82 var foo = bar ? !!baz : !!bat;
89 This rule has an object option:
91 * `"enforceForLogicalOperands"` when set to `true`, in addition to checking default contexts, checks whether the extra boolean cast is contained within a logical expression. Default is `false`, meaning that this rule by default does not warn about extra booleans cast inside logical expression.
93 ### enforceForLogicalOperands
95 Examples of **incorrect** code for this rule with `"enforceForLogicalOperands"` option set to `true`:
100 /*eslint no-extra-boolean-cast: ["error", {"enforceForLogicalOperands": true}]*/
106 while (!!foo && bar) {
110 if ((!!foo || bar) && baz) {
114 foo && Boolean(bar) ? baz : bat
116 var foo = new Boolean(!!bar || baz)
121 Examples of **correct** code for this rule with `"enforceForLogicalOperands"` option set to `true`:
126 /*eslint no-extra-boolean-cast: ["error", {"enforceForLogicalOperands": true}]*/
136 if ((foo || bar) && baz) {
140 foo && bar ? baz : bat
142 var foo = new Boolean(bar || baz)
144 var foo = !!bar || baz;