Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
Created October 9, 2014 15:09
Show Gist options
  • Save lizlongnc/58661316a11f3d661107 to your computer and use it in GitHub Desktop.
Save lizlongnc/58661316a11f3d661107 to your computer and use it in GitHub Desktop.
JS hoisting in functions
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