Here is something interesting I found while learning scope in javascript.
Code
var foo = "This is a global variable.";
var bar = function() {
alert(foo);
foo = "Value has been modified";
}
bar();
alert(foo);
This gives the normal response you think you would get, but if I change this one line:
from:
foo = "Value has been modified";
to:
var foo = "Value has been modified";
I get a value of undefined for foo, why is this? Since the function is global scope how come it doesn't accept the var keyword in front of it?
Edit
Now I understand basically what is happening the var foo in the function bar will gain most importance because of the var keyword and get hoisted up, but it will be hoisted up without the value it has been assigned.