Can anyone explain the differences in the results of the following Shouldn't they all equal 1? since the a1 = 1 remains unchanged in the global scope?
// Case 1: shows 1
var a1 = 1;
function b1() {
a1 = 10; //defines a1 in the local scope
return;
function a1() {} //defines a1 in the local scope
}
b1();
console.log(a1);
// Case 2: shows 10
var a2 = 1;
function b2() {
a2 = 10; //a2 in the local scope
return;
}
b2();
console.log(a2); //a2 in the global scope should remain as 1 but its 10 here???
// Case 3: shows 10
var a3 = 1;
function b3() {
a3 = 10;
return;
function testingblablabla() {} // the same for this. a3 =10 is within b3 and globally a3 is still 1. why does it give a 10?
}
b3();
console.log(a3);