Redundant Boolean Ternary
ID |
javascript.redundant_conditional |
Severity |
low |
Remediation Complexity |
auto_fix |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
code-smell, simplification |
Description
Reports ternary expressions whose two branches are boolean literals with opposite values:
// Bad
const flag = cond ? true : false;
return cond ? false : true;
Rationale
A ternary that picks between true and false based on a condition is just the boolean coercion of that condition — the ternary adds visual noise without adding meaning.
Remediation
Use !!cond to get an explicit boolean, or !cond for the inverse:
// Good
const flag = !!cond;
return !cond;
If cond is already known to be a boolean, the !! is unnecessary:
return cond;
Notes
The rule does not fire when both branches are the same boolean (a different smell, captured separately) or when at least one branch is a non-literal expression.