I'm in javascript for a quite long time now, but i'm not able to understand the following case:
var a = "test1";
var c = function()
{
console.log("2/ "+a)
if(true)
{
console.log("3/ "+a);
}
else
{
var a = "test2";
console.log("3/ "+a);
}
}
console.log("1/ "+a);
c();
=> 1/ test1
=> 2/ undefined
=> 3/ undefined
Why the variable become unavailable on the line 'console.log("2/ "+a)', and 'console.log("3/ "+a);' ?
I understand that once we reach the "else" the global scope of "a" inside function c() is "destroyed", but in my mind, as javascript is "scripted" we must be able to use a before the redefinition in the function scope.
Which mecanism is responsible of that ?