haresh lakhwani thank you for your response sir but, why it's giving a reference error that foo () is not a function. If we do console.log (foo) at the top of the scope it indeed gives us a function in response which is the same inside if block, then why the error mentioning something else? I am not able to figure out that where I am mistaken?
Also, If we agree at this point that it can be only invoked inside if or else function, then how the same code is functioning, if we do this:
if (true) { function foo() { console.log(1); } } else { function foo() { console.log(2); } } foo();