JavaScript actually has a third way to create functions: with the Function()
constructor. Just like function expressions, functions defined with the Function()
constructor are not hoisted.
var func = new Function('n','m','return n+m'); func(2,3); // returns 5
But the Function()
constructor is not only confusing, it is also highly dangerous. No syntax correction can happen, no optimization is possible. It's far easier, safer, and less confusing to write the same function as follows:
var func = function(n,m){return n+m}; func(2,3); // returns 5