Duplicate Parameter Names in JavaScript Strict Mode

In JavaScript, strict mode introduces enhanced error checking and stricter parsing rules to prevent common coding errors and eliminate silent failures. One fundamental restriction introduced by strict mode is the complete prohibition of duplicate parameter names in function definitions. This article breaks down how duplicate parameter names behave in non-strict versus strict mode, the resulting errors, and how modern ECMAScript standards enforce this rule across different function types.

Non-Strict Mode Behavior

In non-strict (sloppy) mode, JavaScript permits standard functions to declare multiple parameters with identical names. When this occurs, the function does not throw an error during definition or invocation.

function add(x, x) {
    return x + x;
}

console.log(add(2, 5)); // Outputs 10 (uses the last value, 5)

In the example above, the second occurrence of x overwrites and shadows the first instance. The first parameter’s value can only be retrieved using the arguments object (arguments[0]). This behavior frequently leads to logic errors and reduces code maintainability.

Strict Mode Restrictions

When strict mode is enabled using the "use strict"; directive, defining duplicate parameter names is treated as a syntax violation. The JavaScript engine detects the duplicate identifier during the parsing phase—before any code executes—and immediately throws a SyntaxError.

"use strict";

function add(x, x) {
    // SyntaxError: Duplicate parameter name not allowed in this context
    return x + x;
}

This restriction applies to: * Function declarations (function fn(a, a) {}) * Function expressions (const fn = function(a, a) {}) * Named function expressions * Methods defined in strict mode contexts

Duplicate Parameters in Modern JavaScript (ES6+)

Modern JavaScript extends these strict restrictions to newer syntax constructs, even when the "use strict"; directive is not explicitly present in the file:

  1. Arrow Functions: Arrow functions do not allow duplicate parameter names under any circumstances. Attempting to define (a, a) => {} results in a SyntaxError.
  2. Method Definitions: Methods defined inside ES6 classes or object literal method shorthand syntax reject duplicate parameters automatically because class bodies are always executed in strict mode.
  3. Complex Parameter Lists: Any function utilizing ES6 parameter features—such as default parameters (a = 1), rest parameters (...args), or destructuring patterns ({ a })—automatically disallows duplicate parameter names, regardless of strict mode settings.

Summary of the Restriction

Strict mode enforces parameter uniqueness to ensure that every named parameter maps unambiguously to a distinct value. By converting what was once an error-prone silent overwrite into a compile-time SyntaxError, strict mode prevents unintended shadowing bugs and aligns parameter declarations with modern lexical scoping rules.