Skip to content

Instantly share code, notes, and snippets.

@gilles-leblanc
Created April 13, 2013 01:29
Show Gist options
  • Save gilles-leblanc/5376445 to your computer and use it in GitHub Desktop.
Save gilles-leblanc/5376445 to your computer and use it in GitHub Desktop.
No block scope and variable hoisting
/** 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%;
<!-- 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>
// 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;
{"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