ESLint v8.x reached end-of-life on 2024-10-05 and is no longer maintained. Upgrade or consider long-term support options
Versions

no-ternary

Disallow ternary operators

The ternary operator is used to conditionally assign a value to a variable. Some believe that the use of ternary operators leads to unclear code.

var foo = isBar ? baz : qux;

Rule Details

This rule disallows ternary operators.

Examples of incorrect code for this rule:

Open in Playground
/*eslint no-ternary: "error"*/

var foo = isBar ? baz : qux;

function quux() {
  return foo ? bar() : baz();
}

Examples of correct code for this rule:

Open in Playground
/*eslint no-ternary: "error"*/

var foo;

if (isBar) {
    foo = baz;
} else {
    foo = qux;
}

function quux() {
    if (foo) {
        return bar();
    } else {
        return baz();
    }
}

Version

This rule was introduced in ESLint v0.0.9.

Resources

Change Language