-1

I was wondering, why the alert result is different after commenting out this line function a() {} ? What is the relationship between function a() and variable a?

Snippet 1:

var a = 1;
function b() {
    a = 10;
    return;
    //function a() {}
}
b();
alert(a); // 10

Snippet 2:

var a = 1;
function b() {
    a = 10;
    return;
    function a() {}
}
b();
alert(a); // 1
Blake
  • 6,867
  • 18
  • 52
  • 76

1 Answers1

0

It is because a is getting redefined inside of b and removes the closure.

Daniel A. White
  • 181,601
  • 45
  • 354
  • 430