On the problem that the output of self-executing function definition function declaration result is undefined

I saw a question today. I was very confused. I never thought of the answer. The question goes like this:

<script>
        function g() {
            console.log(123)
            return true
        }
        ;(function(){
            console.log(g)    //undefined
            console.log(g())    //g is not function
            if(g) {
                function g() {
                    console.log(456)
                    return false
                }
            }
        })()
        console.log(g())
    </script>

in self-executing functions, using function declarations to define function can also increase variables. Since it has been promoted, why is it still undefind or why is it g is not function?? Please tell the gods one or two, thank you

Mar.28,2021
The

function is block-level in newer browsers. Some


you can use the old version of ie to execute this code. Take a look at the results


Block level scope

I suggest you take a look at this, which talks about this issue in detail, which is related to the compatibility of browser implementation and historical legacy

.
Menu