Function with duplicate parameters. Your turn, JS!
Tags: javascript, beginners, function, syntax
Published: 2020-08-18 14:09:59

**Assertion**: We are in parallel universe where all weird code snippets may exist and be used.

Let's imagine the simplest function declaration with 2 parameters... but both parameters have the same name: 

```javascript
function f(a,a){
  console.log(a)
}
```

What reaction would you expect from JS?

Probably there are 2 options:
1) throw some error (we are lucky if it's not runtime error);
2) create valid function `f`

Of course we may just run the code and check but it is too simple. 
Instead, I propose to find the origin of truth and open [ES specification](http://www.ecma-international.org/ecma-262/6.0/#sec-function-definitions) 📕📗📘 

####Inside the ES spec 

As in the snippet we try to create a function, so go to **Function Definitions** section in the spec. Inside we may find the following:

>_FunctionDeclaration_ : **function** _BindingIdentifier_ ( _FormalParameters_ ) { _FunctionBody_ }
_FunctionExpression_ : **function** _BindingIdentifieropt_ ( _FormalParameters_ ) { _FunctionBody_ }
* If the source code matching this production is strict code, the Early Error rules for _StrictFormalParameters_ : _FormalParameters_ are applied.

It means if you try to create a function as function declaration or function expression in `"strict mode"` some additional Early Error(errors on code parsing stage) rule is applied: 

>_StrictFormalParameters_ : _FormalParameters_
* It is a Syntax Error if BoundNames of _FormalParameters_ contains any duplicate elements.

In that context BoundNames are just parameter names. 

So the spec says that if you try to create a function as function declaration or function expression in `"strict mode"` and use the same name for function parameters then JS returns Syntax Error!

```javascript
function f(a,a){
  'use strict'
  console.log(a)
}
```

Just copy and paste the above snippet in your browser console and check the error:

`Uncaught SyntaxError: Duplicate parameter name not allowed in this context`

Rather obviously, right? 

####Ok cool, but what about `non-strict mode`? 

In `non-strict mode` mentioned Early Error rules is not applied to the function declaration or function expression and JS just creates a valid function which you may call later without any errors:

```javascript
function f(a,a){
  console.log(a)
}

f(0,100)
// 100 in console
```

JS freedom is one love!

####Ok cool, but what about arrow function definition?

Let's check the arrow function parameters syntax in the spec:
>_ArrowFormalParameters_ :
( _StrictFormalParameters_ )

It means that duplicate parameters Early Error rule **is always applied** to arrow function definition even if `'strict mode'` is not defined explicitly.

------
####Instead of conclusion:
* Function declaration and function expression with duplicate parameters in `'strict mode'` throw Syntax Error;
* Function declaration and function expression with duplicate parameters in `'non-strict mode'` create a valid function;
* Arrow function definition with duplicate parameters always throws Syntax Error;
* Keep calm and read specs :) 📕📗📘

####P.S
The ES6 spec contains [Annex C](http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-of-ecmascript) - list of the strict mode restriction and exceptions. There is a point about our topic in that list as well.