Below are many examples of function hoisting behavior in JavaScript. Ones marked as works
successfuly print 'hi!' without errors.
To play around with these examples (recommended) clone them with git and execute them with e.g. node a.js
(I may be using incorrect terms below, please forgive me)
When JS is parsed, a first pass is done over each scope, and function definitions are immediately discovered. An example of a function definition is function foo() {}
. When a function is declared like this, with a name, that name becomes available to the entire scope when the code in that scope is executed.
A crude timeline of how JS gets executed:
- Parse the scope and detect all function definitions
- Execute the code top-to-bottom with all functions found in step 1 available as variables
This behavior is called 'hoisting' because it is almost like the function definitions have been 'hoisted' up to the top of the function.
Assignments are not evaluated until the code is executed. An example of an assignment is var foo = function() {}
or var foo = function foo() {}
. A function must not be associated with an assignment in order for it to be hoisted (see example L
)
Wrapping a function in parenthesis (()
) is a quick way to convert a function definition into a function expression, which means it does not get hoisted (similar to assigning the function to a variable). I personally do not use this pattern regularly as I find it overly confusing to newbies etc, but I have included it because it is widely used in e.g. jQuery plugins.
I use hoisting as a code organization tool, for example here I rely on hoisting to make parseStream()
available on line 20, even though it is defined on line 41, which I think makes that file more readable as I can put the 'meat' of the function at the top.