Created
March 4, 2012 16:40
-
-
Save attilammagyar/1973797 to your computer and use it in GitHub Desktop.
JavaScript variable declaration hoisting examples
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>JavaScript variable declaration hoisting</title> | |
<meta charset="utf-8" /> | |
<script type="text/javascript" src="test.js"></script> | |
</head> | |
<body> | |
<h1>See FireBug console :-)</h1> | |
</body> | |
</html> |
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
function redeclared_in_every_iteration() { | |
for (var i = 0; i != 3; ++i) { | |
var a | |
if (i == 1) { | |
a = "not undefined" | |
} | |
console.log(a) | |
} | |
} | |
var foo = "global" | |
function which_is_bound() { | |
function bar() { | |
console.log(foo) | |
} | |
bar() | |
var foo = "local" | |
bar() | |
} | |
console.log("redeclared_in_every_iteration ----------------") | |
redeclared_in_every_iteration() | |
console.log("which_is_bound ----------------") | |
which_is_bound() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment