This question has already been answered:
- Function declaration in if and its rise 2 responses
Why does the function declaration go to the beginning of the block, and its identifier is available already at the beginning of the script with an undefined value?
<script> console.log(foo); // undefined if(true) { console.log(foo()); // "hello" function foo() { return 'hello'; } } console.log(foo()); // "hello" </script>