Duplicated parameter names

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 *