Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt

Use this file to discover all available pages before exploring further.

Deeply nested ternary expressions are hard to read and easy to misinterpret. This rule is an improved version of ESLint’s built-in no-nested-ternary rule: instead of banning all nesting, it allows a single level of nesting as long as the inner ternary is wrapped in parentheses, which makes the structure explicit.
Recommendedrecommended
Fixable🔧 Auto-fixable
Suggestions
// Inner ternary is not parenthesized
const foo = i > 5 ? i < 100 ? true : false : true;

// Two levels of nesting — not allowed regardless of parentheses
const bar = i > 5 ? true : (i < 100 ? true : (i < 1000 ? true : false));

Autofix behavior

The rule can automatically fix the case where a nested ternary is one level deep but missing parentheses:
// Before fix
const foo = i > 5 ? i < 100 ? true : false : true;

// After fix
const foo = i > 5 ? (i < 100 ? true : false) : true;
When nesting goes deeper than one level, the rule reports an error but does not apply an automatic fix — you need to refactor the logic manually, typically by extracting conditions into variables or if/else blocks.

Replacing the built-in rule

Disable ESLint’s built-in no-nested-ternary rule in favor of this one. The recommended preset from eslint-plugin-unicorn already does this for you.
{
  "rules": {
    "no-nested-ternary": "off"
  }
}
When you find yourself needing more than one level of ternary nesting, consider restructuring with if/else or extracting the inner condition into a named variable. Named conditions communicate intent more clearly than chained conditionals.

Build docs developers (and LLMs) love