As you know, all variables, no matter where in the function body they are declared, get
hoisted to the top of the function behind the scenes. The same applies for functions
because they are just objects assigned to variables. The only “gotcha” is that when using
a function declaration, the definition of the function also gets hoisted, not only its
declaration. Consider this snippet:
---------------------------------------------------------------------------------------
In this example you see that, just like with normal variables, the mere presence of  foo
and  bar anywhere in the  hoistMe() function moves them to the top, overwriting the
global  foo and  bar . The difference is that local  foo() ’s definition is hoisted to the top
and works fine; although it’s defined later. The definition of  bar() is not hoisted, only
its declaration. That’s why until the code execution reaches  bar() ’s definition, it’s
undefined and not usable as a function (while still preventing the global  bar() from
being “seen” in the scope chain).
Now that the required background and terminology surrounding functions is out of
the way, let’s see some of the good patterns related to functions that JavaScript has to
offer, starting with the callback pattern. Again, it’s important to remember the two
special features of the functions in JavaScript:
• They are objects.
• They provide local scope.