Skip to content
← Back to rules

unicorn/no-nested-ternary Style

🛠️ An auto-fix is available for this rule for some violations.

What it does ​

Disallow deeply nested ternary expressions.

Nested ternary expressions that are only one level deep and wrapped in parentheses are allowed by this rule.

Why is this bad? ​

Nesting ternary expressions can make code more difficult to understand.

Examples ​

Examples of incorrect code for this rule:

javascript
const foo = i > 5 ? (i < 100 ? true : false) : true;
const foo = i > 5 ? true : i < 100 ? true : i < 1000 ? true : false;

Examples of correct code for this rule:

javascript
const foo = i > 5 ? (i < 100 ? true : false) : true;
const foo = i > 5 ? (i < 100 ? true : false) : i < 100 ? true : false;

How to use ​

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

json
{
  "rules": {
    "unicorn/no-nested-ternary": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-nested-ternary": "error",
  },
});
bash
oxlint --deny unicorn/no-nested-ternary

Version ​

This rule was added in v0.0.18.

References ​