OK, this is driving me crazy:
First example, no problem:
<script>
window.myvar = 150;
if (false) {
var myvar = 3;
}
// This will popup "150"
alert(myvar)
</script>
Now, with TWO script elements:
<script>
window.myvar = 150;
</script>
<script>
if (false) {
var myvar = 3;
}
// This will popup "undefined"
alert(myvar)
</script>
Tested with IE8.
Have you any idea why?
myvar
's definition is being hoisted and shadowing the property ofwindow
. - alexif(false){}
block, because I didn't add it in the code above. Ad@m - kirb