Prefer using a logical operator over a ternary ​
💼 This rule is enabled in the ✅ recommended config.
💡 This rule is manually fixable by editor suggestions.
Disallow ternary operators when simpler logical operator alternatives exist.
Ideally, most reported cases have an equivalent Logical OR(||) expression. The rule intentionally provides suggestions instead of auto-fixes, because in many cases, the nullish coalescing operator (??) should be preferred.
Fail ​
js
foo ? foo : bar;js
foo.bar ? foo.bar : foo.bazjs
foo?.bar ? foo.bar : bazjs
!bar ? foo : bar;Pass ​
js
foo ?? bar;js
foo || bar;js
foo ? bar : baz;js
foo.bar ?? foo.bazjs
foo?.bar ?? baz