Debugging an AngularJS app the other day, I was frustrated because I could not step through a function expression. After researching JavaScript function declarations and expressions, the difference was clear but the reason to use one or the other was not.
Function declaration is standard in other languages so I set out to find the benefits of function expressions. Here are 3 points to shed light on the situation, from least convincing to most convincing.
gist:bradydowling/07b2cc2c1f35778c8a66#file-gistfile1-txt
With expressions, you can appropriately assign different functions to a variable depending on other conditions.
gist:bradydowling/ea419dcf149726244a10#file-gistfile1-txt
The first source below says this is the only reason you should use function expressions and every other function should be declared. While I agree with this, it seems people are leaning toward expressions so the predominant use of declarations may be a dated practice. What are some other reasons you might use function expressions or function declarations?
Update (12/27/14): My second point is incorrect. If you don’t call the function (whether instantiated by declaration or expression) with a set of parens after it then it will just return the function body.