I have done some tests with codes listed below:
function foo(x) {
alert(y);
}
var y = 'I am defined outside foo definition';
foo();
The above code gives me an alert 'I am defined outside foo definition'.
Then another test:
function bar(x) {
alert(x);
}
var x = 'I am defined outside foo definition';
bar();
function bar(x) {
alert(x);
}
x = 'I am defined outside bar definition';
bar();
Both the above codes give me an alert 'undefined'.
So Why?
This is because when you declare the parameter x
You are "hiding" the global variable x. You only have access to the parameter x
try passing in the global x
If we omit the parameter then the global x would become visible again
Note that global variables should be limited in good js applications.