As Michael.Lumley indicated in the answer that it is required in general for the code which defines something to occur before that code is executed. But, Javascript support "hoisting", which enables coder to call a piece of code before it is defined.( more on hoisting - https://developer.mozilla.org/en-US/docs/Glossary/Hoisting )
And hence following code works finely:
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 the jsfiddle - https://jsfiddle.net/px2ghrgy/
But still, your code apparently would not work, because it seems the conditionals, does not support the hoisting as such. And the reason is the scope, i.e., whereas the functions are hoisted in an enclosing scope(either global or inside a function), it does not happen inside a conditional(if/else).
You would find this answer relevant too -> https://stackoverflow.com/a/35250887/7374117