As Michael Lumley noted in the answer, in general, a code requires that something happens before this code is executed. But Javascript support is "hoisting", which allows the encoder to call a piece of code before it is defined (more about lifting - https://developer.mozilla.org/en-US/docs/Glossary/Hoisting ) And, therefore, the following code works fine:
example() function example() { console.log("outside the if block above function b declaration"+b()); if (true) { console.log("inside the if block"+a()); console.log("inside the if block above function b declaration"+b()); } } function a() { return "you invoked function a"; } function b() { return "you invoked function b"; }
Here is jsfiddle - https://jsfiddle.net/px2ghrgy/
But, nevertheless, your code, apparently, will not work, because it seems conditional, does not support lifting as such. And the reason is that the region, that is, while functions rise in the enclosing region (both global and inside the function), this does not happen inside the conditional (if / else). You will also find the corresponding answer β fooobar.com/questions/846015 / ...
Hs
source share