Does if block create a new local scope inside a function scope?

javascript

Solution

Yes and no. The keyword `let` does support a local scope in blocks. The keywords `function` and `var` work on the function level scope. They define an indentifier, when the block is compiled before execution. So you normally can call functions above the declaration.

In your example the function is declared conditionally. It will get declared after the condition is evaluated and before the inner block is executed. But when it gets declared, it is valid in the entire function's scope. Try moving the invokation below the if-block, and it will be known and executed.

Problem

For example: ``` function example() { console.log("outside the if block above function b declaration"+b()); function a() { return "you invoked function a"; } if (true) { console.log("inside the if block"+a()); console.log("inside the if block above function b declaration"+b()); function b() { return "you invoked function b"; } } } ``` When i invoke this example() function, I get an error that b is undefined, but when I remove the 2nd line that invokes with function b defined inside the if block It's all ok?

Original source

Related problems