I tried the following code:
var a = 5;
function x() {
console.log(a);
}
x();
Runs as expected and prints 5.
But I changed the code, so the global variable a will be overwritten as follows:
var a = 5;
function x() {
console.log(a);
var a = 1;
}
x();
It prints undefined. This does not make sense to me, since rewriting should occur immediately after console.log (a). So what is the problem?
user1725316
source
share