var x = 5;
function a() {
function b() {
console.log(x);
}
b();
var x = 6;
}
a();
In console.log(x), output is undefined. But if I change to,
var x = 5;
function a() {
function b() {
console.log(x);
}
b();
x = 6; // remove `var`
}
a();
then output is 5.
Why this behaviour? Please explain me.