Skip to content

noTernary

  • Rule available since: v2.3.8
  • Diagnostic Category: lint/style/noTernary
  • This rule isn’t recommended, so you need to enable it.
  • This rule doesn’t have a fix.
  • The default severity of this rule is information.
  • Sources:
biome.json
{
"linter": {
"rules": {
"style": {
"noTernary": "error"
}
}
}
}

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.

const foo = isBar ? baz : qux;
code-block.js:1:13 lint/style/noTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected ternary operator.

> 1 │ const foo = isBar ? baz : qux;
^^^^^^^^^^^^^^^^^
2 │

Ternary operators can lead to unclear code. Use if-else statement instead.

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