---
description: 🚧 施工中
---

# deplicate parameters

[JS](../../..) ⟩ [objects](../../obj) ⟩ [built-in](../../builtin) ⟩ [Function](../../builtin/func) ⟩ [parameter]() ⟩ duplicate parameters

{% hint style="warning" %}
in <mark style="color:yellow;">**non-strict mode**</mark>, JavaScript allows a function to have several <mark style="color:yellow;">**parameters with the**</mark>**&#x20;**<mark style="color:red;">**same name**</mark>, where <mark style="color:yellow;">**later**</mark> parameters <mark style="color:red;">**shadow**</mark> <mark style="color:yellow;">**earlier**</mark> parameters.
{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %}
⭐️ <mark style="color:purple;">**duplicate parameters**</mark> <mark style="color:red;">**not allowed**</mark> in <mark style="color:yellow;">**strict mode**</mark>.
{% endhint %}

```javascript
function g(x, y, x, y) {
    //           ^
    // ⛔ SyntaxError: 
    //    Duplicate parameter name not allowed in this context
    //    (note: this is a compile-time error)
    
    'use strict';        // ⭐️ strict mode
    
    return [x, y];
}
```

👉 [duplicate-parameter](../../../err/syntax/duplicate-parameter "mention")
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [ydkjs-scope-and-closures-v.2](../../../../master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2 "mention") ⟩ Ch. 1 ⟩ [Required: Two Phases](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch1.md#required-two-phases)
* [ ] [Function With Duplicate Parameters. Your Turn, JS!](https://medium.com/swlh/function-with-duplicate-parameters-your-turn-js-ffd42944dfe4)
* [x] [Duplicate Function Parameters in JavaScript](https://www.codeblocq.com/2016/06/Duplicate-Function-Parameters-in-JavaScript/)
{% endtab %}

{% tab title="💈範例" %}
* replit：[duplicate parameters](https://replit.com/@pegasusroe/duplicate-parameters#index.js)

```javascript
// function with duplicate parameters
function f(x, y, x, y) {
    return [x, y];
}

//    x = 3
//      ⭡ ↱ y = undefined
f(1, 2, 3  )                    // [ 3, undefined ]
```
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}
<mark style="color:purple;">**duplicate parameters**</mark>&#x20;

```javascript
mode         FD      FE      AF
----------------------------------
strict       ⛔️      ⛔️      ⛔️         // ⛔️ SyntaxError
non-strict   ✅      ✅      ⛔️         // ✅ valid
----------------------------------
• FD: function declaration, 
• FE: function expression, 
• AF: arrow function
```
{% endhint %}

{% hint style="warning" %}
<mark style="color:purple;">**duplicate parameters**</mark> <mark style="color:orange;">**Early Error rule**</mark> (🚧) is <mark style="color:red;">**always**</mark> applied to [arrow](../arrow "mention") definition even if '<mark style="color:yellow;">**strict mode**</mark>' (🚧) is <mark style="color:red;">**not**</mark>**&#x20;**<mark style="color:yellow;">**defined explicitly**</mark>.&#x20;
{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}
* <mark style="color:purple;">**duplicate parameters**</mark> can cause [early-errors](../../../err/compile/early-errors "mention")❗️
{% endtab %}

{% tab title="🗣 討論" %}
* [Javascript function declaration with same arguments](https://stackoverflow.com/questions/36131750/javascript-function-declaration-with-same-arguments)
{% endtab %}

{% tab title="❓" %}
{% hint style="warning" %}
why does JavaScript even allow you to do such an obviously stupid thing in the first place?
{% endhint %}
{% endtab %}

{% tab title="🚧 " %}
{% hint style="warning" %}
* [x] strict mode
* [ ] Early Error rule
{% endhint %}
{% endtab %}
{% endtabs %}
