Consider the following example of shadowing in JavaScript:
let a = 99;
{
var a = 10;
let b = 11;
const c = 200;
console.log(a);
}
console.log(a);
Here I get the following error:
SyntaxError: Identifier 'a' has already been declared
But in the following case, there is no syntax error and the code is completely valid.
var a = 99;
{
let a = 10;
let b = 11;
const c = 200;
console.log(a);
}
console.log(a);
In the second case var a is declared in global scope and let a is in block scope.
But why this is not valid in first case?
There, let a will be declared in separate scope and var a should be declared in the global scope.
Why this case is invalid?