I\'ve been learning Javascript using Object-Oriented JavaScript by Stoyan Stefanov
He offers an example comparing global and local scope:
var a = 123
It is not overriding the global variable. What is happening is called "variable hoisting". That is, a var a;
gets inserted at the top of the function.
The script engine changes your script to be the following:
var a = 123;
function f() {
var a;
alert(a);
a = 1;
alert(a);
}
f();
Lesson to learn: Always declare your variables before you use them. Some will say declare all your variables at the top of the function.