One of my friends was taking an online quiz, and he asked me this question, which I could not answer.
var global = false; function test() { global = true; return false; function global() {} } console.log(global);
Assuming functions rise above with var variables, try this.
var foo = 1; function bar() { return foo; foo = 10; function foo() {} var foo = 11; } bar(); console.log(foo);
Here is the JSBin Demo and JSBIN Demo2 to play with.
PS: If we remove function global() {} from test() , then it works fine. Can someone help me understand why this is happening?
javascript scope global-variables
sachinjain024
source share