Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does local variable kill my global variable?

Sorry for this question, but this issue really screwed up my day.

The following Code alerts 10 as it should:

var globalId='10';   function check(){       alert(globalId);   }   check(); 

But this next code alerts undefined:

var globalId='10';   function check(){       alert(globalId);      var globalId;  }   check(); 

I am aware that if I declare a variable in a function its a local variable, but if I already declared it as global, how can it be that my alerts says undefined?

This is an easy example, but in my original code I did a lot of stuff in between the beginning of the function, then a long way down I checked to see if globalId was defined, else define it: if(!globalId){var globalId;} This meant that my alert situated at the top of the function generated undefined, as if JavaScript first executed the whole function, just to see if any variables 'might' be declared, and if so, declare them and therefore my alert pointed to an 'undeclared' variable.

Can anybody explain to me why this happen, and if it is true that JavaScript "pre-declares" all variables before executing a function, even variables declared in conditions not even met?

like image 316
Per Spjuth Avatar asked Mar 31 '11 11:03

Per Spjuth


People also ask

Do local variables override global variables?

LOCAL variables are only accessible in the function itself. So, in layman's terms, the GLOBAL variable would supersede the LOCAL variable in your FIRST code above.

Why do programmers hate global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

What happens when there is conflict between a local and global variable?

In case of a conflict between the names of a local and global variable what happ. In case of a conflict between the names of a local and global variable what happens? A The global variable is given a priority.

What is the biggest issue you will get into when using local variables?

Avoiding Local Variables and Debugging Local variables will eventually be useful but for now they're most likely to just be confusing. The biggest issue you'll run into right now with local variables is accidentally using var inside of an onEvent or function .


2 Answers

In javascript, you should know there is something called as HOISTING.

What this essentially means is, when you declare any local variables, the variable declaration is automatically carried to top of the scope.

eg:-

var globalId='10'; function check(){ alert(globalId); var globalId; } check();  

Changes to -

var globalId='10'; function check(){ var globalId; alert(globalId);} check();  

Since globalID is still not assigned any value, it returns undefined in your output. The local variables always get priority over the global variables with same name.

like image 102
Sachin Shanbhag Avatar answered Sep 20 '22 19:09

Sachin Shanbhag


Yes, all variables declared anywhere in a function are local to that function and exist throughout the function's code; they will be used in preference to globals of the same name.

From https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Variable_Scope :

JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within. [...] Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement.

like image 41
Gareth McCaughan Avatar answered Sep 21 '22 19:09

Gareth McCaughan