JavaScript hoisting example reloaded
<head>
<title>Hoisting example reloaded</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.