Skip to content

Instantly share code, notes, and snippets.

@attilammagyar
Created March 4, 2012 16:40
Show Gist options
  • Save attilammagyar/1973797 to your computer and use it in GitHub Desktop.
Save attilammagyar/1973797 to your computer and use it in GitHub Desktop.
JavaScript variable declaration hoisting examples
<!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>
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