I could not find an exact match on my issue, although many questions on the javascript scope really exist. Here is my current code for the question.
var my_var = "blank";
var MyFunc = function() {
my_var = "one";
}
alert(my_var);
MyFunc();
alert(my_var);
When I run this, I am warned with "empty" and then "one" as expected. However, if I uncomment this line, it looks like this.
var my_var = "blank";
var MyFunc = function() {
my_var = "one";
var my_var = "two";
}
alert(my_var);
MyFunc();
alert(my_var);
I am warned with "blank" and then "blank" . This is not what I would expect, and I am confused that adding a line will remove the behavior. Can someone explain what is going on here? I see this behavior in both firefox and safari.