Last active
April 25, 2016 13:42
-
-
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.
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
// 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