Created
October 9, 2014 15:09
-
-
Save lizlongnc/58661316a11f3d661107 to your computer and use it in GitHub Desktop.
JS hoisting in functions
This file contains hidden or 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
hoisting in functions concern variables | |
The var statement gets spit into two parts: | |
The declaration part gets hoisted to the top of the function, initializing with undefined. | |
The initialization part turns into an ordinary assignment. | |
var myVar = 0, myOtherVar; | |
Expands into | |
var myVar = undefined, | |
myOtherVar = undefined; | |
myVar = 0; | |
Rule of thumb: declare and initialize vars at the beginning of a function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment