Skip to content

Instantly share code, notes, and snippets.

@tennisonchan
Last active April 25, 2016 13:42
Show Gist options
  • Save tennisonchan/a4db3170368eaf60b12b to your computer and use it in GitHub Desktop.
Save tennisonchan/a4db3170368eaf60b12b to your computer and use it in GitHub Desktop.
Demo how variable declarations are hoisted. Regardless of where they occur in the function scope, it will be hoisted to the top. That's why all the variables should be declare at the top.
// Regardless of where they occur in the function scope,
// it will be hoisted to the top.
// it looks like this
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar();
// => alert 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment