Variable promotion is the promotion of a variable to the top of the function. It is important to note that variable promotion is only a declaration of variable promotion and does not promote assignment as well. \

Function promotion is bringing the whole function up front. Function expressions cannot be promoted; function declarations can be promoted.

<! DOCTYPEhtml>
<html lang="en">
<head>
</head>
<body>
<script>
    // The variable is promoted
    function test(){
        a=2;
        var a;
        console.log(a);   / / 2
    }

    test();

    // If the variable is promoted, the assignment will not be promoted
    var v='Hello World';
    (function(){
        var v;
        console.log(v);  //undefined
        v='I love you'; }) ();// The function declaration form can be improved
    function myTest(){
        foo();
        function foo(){
            console.log("hello world");  //hello world
        }
    }
    myTest();

    // Function expressions cannot be promoted
    function myTest2(){
        foo();    //foo is not a function
        var foo =function foo(){
            console.log("hello world");
        }
    }
    myTest2();
</script>
</body>
</html>
Copy the code

\

\

<! DOCTYPEhtml>  
<html lang="en">  
<head>  
</head>  
<body>  
<script>  
// Output Goodbye Jack
var name = 'World! ';
(function () {
    if (typeof name === 'undefined') {
        var name = 'Jack';
        console.log('Goodbye ' + name);
    } else {
        console.log('Hello ' + name);
    }
})();


// Output Hello World!
var name2 = 'World! ';
(function () {
    if (typeof name2 === 'undefined') {
        name2 = 'Jack';
        console.log('Goodbye ' + name2);
    } else {
        console.log('Hello ' + name2);
    }
})();

// Output Hello World!
var name3 = 'World! ';
(function () {
    if (typeof this.name3 === 'undefined') {
        var name3 = 'Jack';
        console.log('Goodbye ' + name3);
    } else {
        console.log('Hello ' + this.name3);
    }
})();
</script>  
</body>  
</html
Copy the code

\