In JavaScript, variables created within a function only have scope only within that function: if that's the case, why does the following code produce the output 1? Surely x
cannot be accessed from outside of foo
?
function foo(){
x = 1;
}
foo()
console.log(x) // '1'