src/content/docs/linter/rules/no-useless-ternary.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.5.0` - Diagnostic Category: [`lint/complexity/noUselessTernary`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-unneeded-ternary`](https://eslint.org/docs/latest/rules/no-unneeded-ternary){
"linter": {
"rules": {
"complexity": {
"noUselessTernary": "error"
}
}
}
}
Disallow ternary operators when simpler alternatives exist.
It’s a common mistake in JavaScript to use a conditional expression to select between two
boolean values instead of using the logical NOT (!) or double NOT (!!) to convert the test to a boolean.
var a = x ? true : true;
var a = foo === 1 ? false : true;
var a = foo + 1 ? false : true;
var a = foo + 1 ? true : false;
var a = x === 2 ? 'Yes' : 'No';
var a = x === 2 ? 'Yes' : false;
Logical NOT: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT