no-unneeded-ternary
eslint
Safe autofix
eslint.config.js
export default [
{
rules: {
"no-unneeded-ternary": "error",
},
},
];Disallow ternary operators when simpler alternatives exist
const a = x === 2 ? true : false;
const b = x ? true : false;const a = x === 2 ? "Yes" : "No";
const b = x !== false;
const c = x ? "Yes" : "No";
const d = x ? y : x;
f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below.Examples from the ESLint documentation.
no-unneeded-ternary
eslint
export default [
{
rules: {
"no-unneeded-ternary": "error",
},
},
];no-unneeded-ternary
eslint plugin · suspicious
{
"rules": {
"no-unneeded-ternary": "error"
}
}complexity/noUselessTernary
complexity · since 1.5.0
{
"linter": {
"rules": {
"complexity": {
"noUselessTernary": "error"
}
}
}
}Disallow use of the RegExp constructor in favor of regular expression literals
Enforce default clauses in switch statements to be last
Disallow the use of variables before they are defined