> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/func/param/duplicate.md).

# deplicate parameters

🚧 施工中

[JS](/web/js.md) ⟩ [objects](/web/js/val/obj.md) ⟩ [built-in](/web/js/val/builtin.md) ⟩ [Function](/web/js/val/builtin/func.md) ⟩ [parameter](/web/js/val/func/param.md) ⟩ 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 not allowed in strict mode❗️](/web/js/err/syntax/duplicate-parameter.md)
{% endtab %}

{% tab title="📗 參考" %}

* [ ] [YDKJS: Scope & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) ⟩ 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 function](/web/js/val/func/arrow.md) 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](/web/js/err/compile/early-errors.md)❗️
  {% 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 %}
