var a = 1
function b() {
a = 20
return
function a() {}
}
b()
console.log(a)
I thought the output would be 20 but its 1 ? The execution context of function b would access variable a from the GEC and update its value imo. Why is it so?
var a = 1
function b() {
a = 20
return
function a() {}
}
b()
console.log(a)
I thought the output would be 20 but its 1 ? The execution context of function b would access variable a from the GEC and update its value imo. Why is it so?
Copyright © 2021 Jogjafile Inc.
function declarations declare a variable sharing the name of the function in the current scope (like
vardoes) and are hoisted.function atherefore creates a local variableainsidefunction band is that that variable that you overwrite.Some people prefer to use arrow functions with explicit variable declarations. That creates an error instead of having the behaviour that surprised you.