Skip to main content

js/noNestedTernary

disallow nested ternary expressions

ESLint Equivalent: no-nested-ternary

Examples

Invalid

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

 file.ts:1:24 lint/js/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Nesting ternary expressions can make code more difficult to
    understand.

    let thing = foo ? bar : baz === qux ? quxx : foobar;
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^


let thing = foo ? bar ? boo : foo : baz ? boo : foo;

 file.ts:1:36 lint/js/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Nesting ternary expressions can make code more difficult to
    understand.

    let thing = foo ? bar ? boo : foo : baz ? boo : foo;
                                        ^^^^^^^^^^^^^^^

 file.ts:1:18 lint/js/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Nesting ternary expressions can make code more difficult to
    understand.

    let thing = foo ? bar ? boo : foo : baz ? boo : foo;
                      ^^^^^^^^^^^^^^^


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

 file.ts:1:6 lint/js/noNestedTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Nesting ternary expressions can make code more difficult to
    understand.

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

Valid

let thing = foo ? bar : foobar;
let thing = foo ? bar || boo : foo || bar;
let thing = foo ? bar && boo : foo && bar;
let thing = foo || baz ? bar || boo : foo || bar;
let thing = foo && baz ? bar || boo : foo && bar;
let thing = foo || baz ? bar || boo : foo && bar;
if (foo) {
	thing = bar;
} else if (baz === qux) {
	thing = quxx;
} else {
	thing = foobar;
}