Function Declaration [ function functionName() {} ] vs. Function Expression [ var functionName = function() {} ]
I had previously thought function functionName () {} was syntactic sugar for var functionName = function () {} . There are some subtle differences in both which I am listing below :- 1) Function Declaration can be placed anywhere in the code. Even if it is invoked before the definition appears in code, it gets executed as function declaration is committed to memory or in a way it is hoisted up, before any other code in the page starts execution. Take a look at the function below: function outerFunction () { function foo () { return 1 ; } return foo (); function foo () { return 2 ; } } alert ( outerFunction ()); //displays 2 This is because, during execution, it looks like:- function foo () { // first function declaration is moved to top return 1 ; } function foo () { // second function declaration is moved to top return 2 ; } function out...