Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables defined in global scope with identical names

Tags:

javascript

Can anybody explain, why next js code rises two alert windows with 'string1' text rather than to rise the second with 'undefined' text inside?? If both variables are described in the same scope..

var a = 'string1';  
alert(a);  
var a;  
alert(a);​

http://jsfiddle.net/FdRSZ/1/

Thanks

like image 863
Max Marchuk Avatar asked Jul 26 '12 14:07

Max Marchuk


People also ask

Can global variables have the same name?

It is usually not a good programming practice to give different variables the same names. If a global and a local variable with the same name are in scope, which means accessible, at the same time, your code can access only the local variable.

Can a local variable and a global variable have the same name?

A program can have the same name for local and global variables but the value of a local variable inside a function will take preference. For accessing the global variable with same rame, you'll have to use the scope resolution operator.

Can a function have a parameter with the same name as a global variable?

Yes it works like this as variables of same names are allowed provided that they are in different scopes.

Can we declare two variables with the same name in different scopes?

Yes, variables belonging to different function can have same name because their scope is limited to the block in which they are defined. Variables belonging to different functions in a single code can have same name if we declare that variable globally as shown in the following code snippet below .


1 Answers

Variable declarations (and function declarations) are hoisted to the top of the scope in which they appear. Assignments happen in place. The code is effectively interpreted like this:

var a;
var a;
a = 'string1';

For example, consider what happens if you declare a variable inside an if statement body:

console.log(myVar); //undefined (NOT a reference error)
if (something === somethingElse) {
    var myVar = 10;
}
console.log(myVar); //10

Because JavaScript does not have block scope, all declarations in each scope are hoisted to the top of that scope. If you tried to log some variable that was not declared, you would get a reference error. The above example is interpreted like this:

var myVar; //Declaration is hoisted
console.log(myVar);
if (something === somethingElse) {
    myVar = 10; //Assignment still happens here
}
console.log(myVar);

So even if the condition evaluates to false, the myVar variable is still accessible. This is the main reason that JSLint will tell you to move all declarations to the top of the scope in which they appear.


In slightly more detail... this is what the ECMAScript 5 spec has to say (bold emphasis added):

For each VariableDeclaration and VariableDeclarationNoIn d in code, in source text order do

  • Let dn be the Identifier in d.
  • Let varAlreadyDeclared be the result of calling env’s HasBinding concrete method passing dn as the argument.
  • If varAlreadyDeclared is false, then
    • Call env’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments.
    • Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments.

So, if a binding already exists with the identifier we are trying to bind now, nothing happens.

like image 54
James Allardice Avatar answered Oct 14 '22 01:10

James Allardice