var foo = 1;
function bar() {
foo = 10;
console.log(foo); // 10
return;
function foo() {}
}
bar();
console.log(foo); // 1
I found this example in one JS test and i don't understand its behavior, i did some research, but I found nothing about that. How global variable 'foo' changes it value to 10 and then changes it back to 1 because of function and how even code after return is reached? When i remove 'foo' function, the global variable changes to 10 normaly. Sorry, i'm new in JS, maybe for you guys its easy, but not for me. Can someone explain me this begavior, please?