Case 1: The following Code alerts 10 as it should:
var globalId='10';
function check(){
alert(globalId);
}
check();
Case 2: But this next code alerts undefined:
var globalId='10';
function check(){
alert(globalId);
var globalId;
}
check();
For Case 2 the solution is :
var globalId='10';
function check(){
var globalId; /**moved to top of the function due to hoisting and has an initial value of undefined. Hence, alert(globalId) return undefined. **/
alert(globalId); //undefined
var globalId;
}
check();
My question is now, how does in Case 1 globalId
has the value of 10
?