Why is this global variable undefined inside a function if the same global variable is re-declared and defined inside that same function?
var a = 1;
function testscope(){
console.log(a, 'inside func');
//var a=2;
};
testscope();
console.log(a, 'outside func');
output:
1 "inside func"
1 "outside func"
Consider same code where var a = 2; inside function block is uncommented
var a = 1;
function testscope(){
console.log(a, 'inside func');
var a=2;
};
testscope();
console.log(a, 'outside func');
Output
undefined "inside func"
1 "outside func"