Understanding “Duplicated Parameter Names” in JavaScript
๐ Introduction
In JavaScript, function parameters must have unique names. If you define a function with duplicate parameter names, it can lead to unexpected behavior and errorsโespecially in strict mode.
๐ What You’ll Learn
- What causes the “Duplicated Parameter Names” issue?
- How JavaScript handles duplicate parameters in non-strict mode?
- How strict mode enforces unique parameters?
- Best practices to avoid this issue
- How to fix this error correctly?
1๏ธโฃ What Causes This Error?
This error occurs when you declare multiple function parameters with the same name.
๐น Example of Duplicate Parameters
function example(a, b, a) {
console.log(a); // Which "a" is used?
}
๐ด In strict mode (use strict), this throws an error:
"use strict";
function example(a, b, a) { // SyntaxError: Duplicate parameter name not allowed
console.log(a);
}
โ
In non-strict mode, JavaScript allows this but overwrites the first a:
function example(a, b, a) {
console.log(a);
}
example(1, 2, 3); // Output: 3
๐น The second a overwrites the first one, which may cause bugs.
2๏ธโฃ How JavaScript Handles Duplicate Parameters?
๐ธ Non-Strict Mode (Before ES5)
Before ECMAScript 5 (ES5), JavaScript allowed duplicate parameter names, but it led to unpredictable behavior.
function test(x, y, x) {
console.log(x);
}
test(10, 20, 30); // Output: 30
๐น Here, x was first assigned 10, then overwritten by 30.
๐ธ Strict Mode (use strict) – ECMAScript 5+
With strict mode, JavaScript disallows duplicate parameters.
"use strict";
function test(a, b, a) {
console.log(a);
}
๐ด Error:
SyntaxError: Duplicate parameter name not allowed in strict mode
โ Fix: Ensure all parameters have unique names.
"use strict";
function test(a, b, c) {
console.log(a);
}
3๏ธโฃ Examples of Duplicated Parameter Issues
๐น Example 1: Function Overwriting Variables
function sum(a, b, a) {
return a + b;
}
console.log(sum(2, 3, 5)); // Output: 8
๐น What happens here?
- The first
areceives2. - The second
areceives5, overwriting the first. - The function returns
5 + 3 = 8instead of2 + 3.
โ Fix: Rename Parameters
function sum(a, b, c) {
return a + b + c;
}
console.log(sum(2, 3, 5)); // Output: 10
๐น Example 2: Arrow Functions
Arrow functions also do not allow duplicate parameters.
const multiply = (x, y, x) => x * y;
๐ด Error:
SyntaxError: Duplicate parameter name not allowed in strict mode
โ Fix: Use Unique Parameter Names
const multiply = (x, y, z) => x * y * z;
๐น Example 3: Function Expressions
Function expressions also follow strict mode rules.
const myFunction = function(a, b, a) {
console.log(a);
};
๐ด Error in strict mode:
SyntaxError: Duplicate parameter name not allowed
โ Fix: Rename Parameters
const myFunction = function(a, b, c) {
console.log(a);
};
4๏ธโฃ Best Practices to Avoid Duplicate Parameters
โ 1. Use Descriptive Parameter Names
Bad:
function calculate(a, b, a) { ... }
Good:
function calculate(price, discount, tax) { ... }
โ
2. Always Use "use strict";
This forces JavaScript to catch duplicate parameters.
"use strict";
function example(a, b, a) { } // โ SyntaxError
โ 3. Refactor the Code
If you accidentally use duplicate parameters, restructure the function.
Before:
function process(x, y, x) { return x + y; }
After:
function process(a, b, c) { return a + b + c; }
โ 4. Use Object Destructuring Instead
Instead of using multiple parameters, use objects:
function calculate({ price, discount, tax }) {
return price - discount + tax;
}
๐น This approach avoids parameter conflicts.
5๏ธโฃ Summary & Best Practices
| Concept | Key Takeaway |
|---|---|
| Duplicate parameters | Not allowed in strict mode ("use strict") |
| Non-strict mode | Allows duplicate parameters but can cause bugs |
| Overwriting issue | The last duplicate parameter overwrites the first |
| Best practice | Always use unique, meaningful parameter names |
Use "use strict"; | Catches this issue early |
| Alternative | Use objects instead of many parameters |
๐ฏ Final Takeaways
โ
Always use unique parameter names.
โ
Enable "use strict"; to prevent errors.
โ
Refactor duplicate parameters to avoid overwriting.
โ
Use object destructuring for complex function arguments.
Would you like more examples or explanations?
