13

I am learning javascript myself. I found if I declare a function with same arguments it just working fine:

function func(a, b, a){
  return b;
}
alert(func(1,2,3));

But if I do this :

function func(a, b, a = 5){
  return b;
}
alert(func(1,2,3)); 
//Firebug error - SyntaxError: duplicate argument names not allowed in this context

Then its not working anymore. What is the logic behind that it was working for first equation but not for second one ?

1
  • You should try using strict mode for these. JSFiddle Commented Mar 21, 2016 at 13:08

4 Answers 4

7

ES2015 (the newest stable spec for the language) allows parameters to be declared with default values. When you do that, the language won't allow you to re-use a parameter name.

When you're not doing any parameter defaults, the language allows the old "sloppy" re-use of parameter names. If you enable "strict" mode interpretation, you'll get an error for your first example too.

Sign up to request clarification or add additional context in comments.

3 Comments

But into strict mode I can declare same variable twice (I just tested). But its not allowing same for function arguments.
@Marymon yes, that's correct. Redundant var declarations are allowed, I suspect because too much old code would break, and because (thanks to the semantics of var) they don't really hurt anything. JavaScript has a long, strange history, and there are many things about it that may seem inconsistent.
It's essentially undefined variable being assigned to other undefined variable issue internally.
3

As per the spec

  1. If parameterNames has any duplicate entries, let hasDuplicates be true. Otherwise, let hasDuplicates be false.

21.b

NOTE Early errors ensure that duplicate parameter names can only occur in non-strict functions that do not have parameter default values or rest parameters.

So, your JS engine ensures that if one of the parameter has default values and hasDuplicates is true then it throws an error.

Comments

1

According to MDN, this kind of check is done by JS internally in case of defaults

function go() {
  return ":P"
}

function withDefaults(a, b = 5, c = b, d = go(), e = this, 
                      f = arguments, g = this.value) {
  return [a,b,c,d,e,f,g];
}
function withoutDefaults(a, b, c, d, e, f, g){
  switch(arguments.length){
    case 0:
      a
    case 1:
      b = 5
    case 2:
      c = b
    case 3:
      d = go();
    case 4:
      e = this
    case 5:
      f = arguments
    case 6:
      g = this.value;
    default:
  }
  return [a,b,c,d,e,f,g];
}

withDefaults.call({value:"=^_^="});
// [undefined, 5, 5, ":P", {value:"=^_^="}, arguments, "=^_^="]


withoutDefaults.call({value:"=^_^="});
// [undefined, 5, 5, ":P", {value:"=^_^="}, arguments, "=^_^="]

Now in your case, this is something like this -

case 0:
    a
case 1:
    b
case 2:
    a = a

But when executing case 2, a is still not defined, and hence it through in error scenario.

See details here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters

Comments

0

Argument name must be unique; if you use same name for two arguments and then interpreter get confuses which one you want to access;

Same you added in code as comment

//Firebug error - SyntaxError: duplicate argument names not allowed in this context

"Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed." Default parameter in ES2015

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.