The code:
var a=1
console.log(a)
if(true){
a=2
console.log(a)
function a(){}
console.log(a)
a=3
console.log(a)
}
console.log(a) // 2 why?
Who please explain why the final result here of “a” is 2?
I have read a related question: What are the precise semantics of block-level functions in ES6? The answer is excellent. But here I still can not understand my code……
How does the function a(){}
work in the block-scope when I have already declared a variable of the same name(a
) in its function-scope?
Anyway, tell me why the variable a
was assigned the value of 2
, please.