Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function declaration with same arguments

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 ?

like image 585
Marymon Avatar asked Mar 21 '16 13:03

Marymon


1 Answers

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.

like image 131
Pointy Avatar answered Oct 19 '22 07:10

Pointy