Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the same variable and parameter names allowed in a javascript function?

Tags:

javascript

As an example am I allowed to use the same variable and parameter? What issues can I run into?

Sample code

function mytask(name,title){
            var name = name;
            var title = title;
            var showalert = ("Hi " + name + " your job title is " + title);
            console.log(showalert);
            return showalert;
        }

document.write(mytask("dan", "administrator"));
like image 706
PeanutsMonkey Avatar asked Jun 19 '12 23:06

PeanutsMonkey


People also ask

Can a function and a variable have the same name in JavaScript?

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

What happens if parameter name is same as variable name?

Answer. When a parameter has the same name as a variable defined outside, instead of the function using the variable defined outside, it will only reference the value that was passed to the parameter. So, parameters will be used over variables of the same name within a function.

Can function parameters have same name?

Yes, the names of the variables you pass in a function call can be the same as the names of the parameters in the function definition.

Can parameters and arguments have the same name?

This name is used within the method body to refer to the passed-in argument. The name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor.


2 Answers

Well in javascript you can think that, scopes are defined my curly brackets: { And }, and inside a scope variables can be redefined, so look at:

function x(){
  var name=3;
  var name=4;
  console.log(name); // output is: 4
}
x();

But this is just half true, actually what happens is that the interpreter goes over the code, and moves all var statements to the beginning, while they're assigned an undefined (and all arguments are defined and taken from stack), and then the code you wrote will run. So any var after the first is simply ignored. And the code you wrote is equal to:

function mytask(name,title){
   var name = arguments[0];
   var title = arguments[1];
   name = name;
   title = title;
   var showalert = ("Hi " + name + " your job title is " + title);
   console.log(showalert);
   return showalert;
}

document.write(mytask("dan", "administrator"));

So your re-deceleration, and assignment is redundant. Anyway - the scope is not changing, nothing else will differ.

Edit

The interpreter goes over your code, with executing anything, any var x = y; statement will split into var x = undefined; and x=y;. And the var x = undefined; will be moved to the top of the code. And the x=y; will be at the same place as the original statement. If you didn't understand the stuff about the stack, don't bother, that's how compilers convert function calls to assembly - it's worth knowing in case you have time; but not THE important thing here.

Anyway - just after those changes, and maybe some optimizations are made, the resulting code is executed. This is not the code you wrote, but an equal one. What you pointed out in redefining the arguments is an edge case where this transformations become visible.

like image 159
Ramzi Khahil Avatar answered Oct 13 '22 21:10

Ramzi Khahil


Think about it this way:

var name = name;

The only way name can be set to the value of name is if name is already defined. There's no need to do it twice if name already has the value you want.

like image 29
Blender Avatar answered Oct 13 '22 21:10

Blender