Back to Eslint

no-nested-ternary

docs/src/rules/no-nested-ternary.md

10.3.0754 B
Original Source

Nesting ternary expressions can make code more difficult to understand.

js
const foo = bar ? baz : qux === quxx ? bing : bam;

Rule Details

The no-nested-ternary rule disallows nested ternary expressions.

Examples of incorrect code for this rule:

::: incorrect

js
/*eslint no-nested-ternary: "error"*/

const thing = foo ? bar : baz === qux ? quxx : foobar;

foo ? baz === qux ? quxx() : foobar() : bar();

:::

Examples of correct code for this rule:

::: correct

js
/*eslint no-nested-ternary: "error"*/

const thing = foo ? bar : foobar;

let otherThing;

if (foo) {
  otherThing = bar;
} else if (baz === qux) {
  otherThing = quxx;
} else {
  otherThing = foobar;
}

:::

Options

This rule has no options.