Greetings.
Appreciate some help to clear up my understanding of variable scope with nested function. With the code below, the error returned is “b is undefined”.
When function g is called at Line 2, why does it not have the visibility of variable b at line 1, since it is called within the scope of variable b?
Thank you very much and much appreciated.
Code
const g = (a) => {
return a + b
}
const f = () => {
const b = 2 // Line 1
return g(1) // Line 2
}
console.log( f() ) // b is undefined