Skip to content

unicorn/prefer-logical-operator-over-ternary Style

🚧 An auto-fix is still under development.

What it does

This rule finds ternary expressions that can be simplified to a logical operator.

Why is this bad?

Using a logical operator is shorter and simpler than a ternary expression.

Examples

Examples of incorrect code for this rule:

javascript
const foo = bar ? bar : baz;
console.log(foo ? foo : bar);

Examples of correct code for this rule:

javascript
const foo = bar || baz;
console.log(foo ?? bar);

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-logical-operator-over-ternary
json
{
  "rules": {
    "unicorn/prefer-logical-operator-over-ternary": "error"
  }
}

References

Released under the MIT License.