Duplicate Function Argument

ID

javascript.function_arguments_uniqueness

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

reliability, suspicious-comparison

Description

Reports function declarations whose parameter list contains the same name twice — function f(x, y, x) { …​ }. The duplicate parameter silently shadows the earlier one; the function body sees only the last value passed for that name. In strict mode (which includes ES modules and class bodies) the duplicate is a SyntaxError.

Rationale

  • The function reads as if it has three independent parameters but only two slots actually feed the body.

  • Tests rarely catch the bug because the duplicated slot usually receives the same value at all call sites.

  • The runtime makes the bug visible only when callers start passing different values, by which point the regression is already in production.

// Bad - third `x` overrides the first
function add(x, y, x) {
  return x + y;
}

Remediation

Rename the duplicate to the value it actually represents, or drop it entirely if it was unintended.

// Good
function add(x, y, override) {
  return (override ?? x) + y;
}