Pointless Arithmetic
ID |
javascript.pointless_arithmetic |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
code-smell, simplification |
Description
Reports an arithmetic expression whose result is mathematically the same as one of its operands:
const a = x + 0; // == x
const b = 0 + x; // == x
const c = x - 0; // == x
const d = x * 1; // == x
const e = 1 * x; // == x
const f = x / 1; // == x
Rationale
These shapes are almost always typos: the developer meant a different operand or a different operator. Even when they are intentional (e.g. as a poor man’s numeric coercion), there are clearer JavaScript idioms — Number(x) or unary +x for coercion, parentheses for precedence — that document the intent.
The expression also adds a small but real cost: a reader has to confirm the arithmetic identity for every occurrence, every time.
Remediation
Drop the no-op operand:
const a = x;
If the goal is numeric coercion, switch to a self-documenting form:
const a = Number(x); // explicit coercion
const a = +x; // shorter, equivalent
Notes
The rule deliberately does not fire on:
-
x * 0and0 * x— sometimes used to coerce-then-zero an unknown value -
0 - x— equivalent to unary-x, a deliberate negation -
1 / x— the reciprocal, often the actual intent
BigInt forms (x + 0n, x * 1n, etc.) are detected through the same numeric-literal recognition.