So I was testing scope of JavsScript (var, let, and const), and I found a weird behavior that I couldn't really understand.
var write = document.getElementById('demo');
function test() {
var m = 'hello';
}
test();
write.innerHTML = m;
//This doesn't work
In above case, JS cannot accesses var m declared locally from global scope.
var write = document.getElementById('demo');
if(true) {
var m = 'hello';
}
write.innerHTML = m;
//This works
In above case, JS accesses var m declared locally from global scope.
Can someone explain why that is..?