src/content/docs/linter/rules/no-constant-binary-expressions.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/suspicious/noConstantBinaryExpressions`](/reference/diagnostics#diagnostic-category) - 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**](/reference/diagnostics#information). - Sources: - Same as [`no-constant-binary-expression`](https://eslint.org/docs/latest/rules/no-constant-binary-expression){
"linter": {
"rules": {
"suspicious": {
"noConstantBinaryExpressions": "error"
}
}
}
}
Disallow expressions where the operation doesn't affect the value
Comparisons which will always evaluate to true or false and logical expressions
(||, &&, ??) which either always short-circuit or never short-circuit are both likely
indications of programmer error.
const value1 = +x == null;
const value2 = condition ? x : {} || DEFAULT;
const value3 = !foo == null;
const value4 = new Boolean(foo) === true;
const objIsEmpty = someObj === {};
const arrIsEmpty = someArr === [];
const shortCircuit1 = condition1 && false && condition2;
const shortCircuit2 = condition1 || true || condition2;
const shortCircuit3 = condition1 ?? "non-nullish" ?? condition2;
const value1 = x == null;
const value2 = (condition ? x : {}) || DEFAULT;
const value3 = !(foo == null);
const value4 = Boolean(foo) === true;
const objIsEmpty = Object.keys(someObj).length === 0;
const arrIsEmpty = someArr.length === 0;