I don't understand why a var
variable can be reassigned within a function, but the change also applies outside of the function. Why/How?
var c = 1;
function Fn() {
c = 2;
}
Fn();
c; // 2
Why isn't the value 2
limited to the scope of the function?
When I write c = 2
within a function, does the javascript engine automatically hoist a new var c
outside of the function and assigns it the value undefined
, which is then changed to 2
once Fn()
is called?