[Online References]
Javascript Functions
If you don't know or remember the difference between Javascript's function declaration and function expression I recommend reading this document first. To refresh your memory below are the examples of those two - primarily used - ways to create functions in Javascript:
/* This is function declaration */
function foo() {}
/* These are function expressions */
var foo = function() {}
var foo = function foo() {} // A named function expression
How you choose to define your functions is mostly driven by your syntactic taste. However one point worth mentioning here is that just like variables the function declarations are also hoisted to top of scope. For variables only names are hoisted to top, whereas in case of function declaration the entire function body is hoisted. Consider below code and choose the right answer from given choices:
function glue() {
stick();
var stick = function() {
console.log('Red');
};
function stick() {
console.log('White');
};
}
glue();
What would you see on console?