Skip to content

Logic rules

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.

Write && or ||. If the bitwise operation is intended, compare its result with a number.

if (isReady & hasData) reports bitwise. Write if (isReady && hasData).

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.

Compare the difference with a tolerance, such as Math.abs(total - 0.3) < Number.EPSILON, or compare whole numbers of the smallest unit.

if (total === 0.3) reports equality. Write if (Math.abs(total - 0.3) < 1e-9).

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.

Remove the test whose result is known, or move the code so that the test can change the outcome.

if (ready) { if (ready) start(); } reports decided at the second ready.

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.

Check both operands and replace the unintended repetition with the correct value.

total - total reports a finding. Check whether one operand should be the previous total.

Reports negation around equality and ordering comparisons.

On in the all config.

A reader must interpret the comparison and then reverse its meaning.

Use the opposite operator when equivalent. Preserve explicit NaN handling for numeric ordering comparisons.

!(first === second) reports a finding. Use first !== second.

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.

Express the intended condition directly. Preserve value coercion and calls with side effects.

if (ready || false) can use if (ready).