Duplicated parameter names

Loading

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 a receives 2.
  • The second a receives 5, overwriting the first.
  • The function returns 5 + 3 = 8 instead of 2 + 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

ConceptKey Takeaway
Duplicate parametersNot allowed in strict mode ("use strict")
Non-strict modeAllows duplicate parameters but can cause bugs
Overwriting issueThe last duplicate parameter overwrites the first
Best practiceAlways use unique, meaningful parameter names
Use "use strict";Catches this issue early
AlternativeUse 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?

Leave a Reply

Your email address will not be published. Required fields are marked *