Created
April 13, 2013 01:29
-
-
Save gilles-leblanc/5376445 to your computer and use it in GitHub Desktop.
No block scope and variable hoisting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** No block scope and variable hoisting | |
* The first commented line is your dabblet’s title | |
*/ | |
background: #f06; | |
background: linear-gradient(45deg, #f06, yellow); | |
min-height: 100%; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- content to be placed inside <body>…</body> --> | |
<div id="first-result"></div> | |
<div id="second-result"></div> | |
<div id="third-result"></div> | |
<div id="fourth-result"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// x will remain in scope even after we leave the block scope of the if statement | |
if (true) { | |
var x = 6; | |
} | |
// accessing variable defined in another block-scope | |
document.getElementById("first-result").innerHTML = x; | |
// y will not be declared because we still have to run the variable declaration statement | |
if (false) { | |
var y = 4; | |
} | |
document.getElementById("second-result").innerHTML = y; | |
if (true) { | |
z = 7; | |
} | |
// variable is assigned even if it is not declared | |
var z; | |
document.getElementById("third-result").innerHTML = z; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"css"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment