-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
  • 7,367
  • 19
  • 54
  • 80

1 Answers1

0

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445