JavaScript hoisting example reloaded

<head>
<title>Hoisting example</title>
</head>
<body>
    <h1>Hoisting in action</h1>
    <script type="application/javascript">
        var hoisting = "global variable";
        alert("global 'hoisting' var => " + hoisting);
        (function(){
            alert("local 'hoisting' var (BEFORE declaration WILL NOT PICK its global 'shadow' -> undefined !) => " + hoisting);
            var hoisting = "local variable";
            alert("local 'hoisting' var (AFTER declaration WILL HIDE its global 'shadow' -> assigned value !) => " + hoisting);
            // block
            {
                var hoisting = "block variable";
                alert("block 'hoisting' var (AFTER declaration WILL OVERRIDE its local 'shadow' -> assigned value !) => " + hoisting);
            }
            // after block
            alert("local 'hoisting' var REPLACED AFTER BLOCK with its block 'shadow' => " + hoisting);
            // THE moral
            alert("The Hoisting MORAL: ALL local variables (even from blocks) are pre-defined/hoisted by JavaScript Runtime in front of the method body !\nDYI to make it clear !");
        })(); //self-executing function 
    </script>
</body>

Thank you Dev{eloper} Stonez (@devstonez) for the tip.

Free Programming Books are now on Codingmarks.org

We're happy to announce that we've reached and surpassed our goal of 1 Mb #codingmarks, by importing the books from the Free-Programming-Books project Continue reading