Strange Behavior on Global and Local Variable in JavaScript

khelidon

New Member
I tried following code:\[code\]var a = 5;function x() { console.log(a);}x();\[/code\]It runs as expected and prints 5.But i changed the code so the global variable a will be overwrite as follows:\[code\]var a = 5;function x() { console.log(a); var a = 1;}x();\[/code\]It prints undefined. It doesn't make sense for me since the overwrite should be happened right after console.log(a). So what is the problem?
 
Back
Top