Logic rules
no-bitwise-in-condition
Section titled “no-bitwise-in-condition”Reports the only & or | of a file when it joins operands that are not numbers in the test of an if, a loop, or a conditional expression.
On in the all config.
A lone & or | in a condition is usually a mistyped && or ||, which also skips short-circuit evaluation.
What to do
Section titled “What to do”Write && or ||. If the bitwise operation is intended, compare its result with a number.
Example
Section titled “Example”if (isReady & hasData) reports bitwise. Write if (isReady && hasData).
no-floating-point-equality
Section titled “no-floating-point-equality”Reports an exact comparison with a value that binary floating point cannot store exactly, such as total === 0.3, a switch case, or a pair of <= and >= that pins one value.
On in the recommended and all configs.
A decimal such as 0.1 has no exact binary form, so 0.1 + 0.2 === 0.3 is false.
What to do
Section titled “What to do”Compare the difference with a tolerance, such as Math.abs(total - 0.3) < Number.EPSILON, or compare whole numbers of the smallest unit.
Example
Section titled “Example”if (total === 0.3) reports equality. Write if (Math.abs(total - 0.3) < 1e-9).
no-gratuitous-expressions
Section titled “no-gratuitous-expressions”Reports a condition operand whose value an enclosing if statement already decided, and a literal true or false as an if test.
On in the recommended and all configs.
A condition with a known result is dead logic: one branch never runs, or the test does nothing, and the code reads as if it could.
What to do
Section titled “What to do”Remove the test whose result is known, or move the code so that the test can change the outcome.
Example
Section titled “Example”if (ready) { if (ready) start(); } reports decided at the second ready.
no-identical-operands
Section titled “no-identical-operands”Reports identical operands in comparisons, logical expressions, subtraction, division and shifts.
On in the recommended and all configs.
Repeating the same operand often means one side refers to the wrong value.
What to do
Section titled “What to do”Check both operands and replace the unintended repetition with the correct value.
Example
Section titled “Example”total - total reports a finding. Check whether one operand should be the previous total.
no-negated-comparison
Section titled “no-negated-comparison”Reports negation around equality and ordering comparisons.
On in the all config.
A reader must interpret the comparison and then reverse its meaning.
What to do
Section titled “What to do”Use the opposite operator when equivalent. Preserve explicit NaN handling for numeric ordering comparisons.
Example
Section titled “Example”!(first === second) reports a finding. Use first !== second.
no-redundant-boolean
Section titled “no-redundant-boolean”Reports redundant boolean literals in loose comparisons, negations, and logical expressions.
On in the recommended and all configs.
Boolean literals can hide the intended test or make a branch independent of its condition.
What to do
Section titled “What to do”Express the intended condition directly. Preserve value coercion and calls with side effects.
Example
Section titled “Example”if (ready || false) can use if (ready).