I've seen this in a lot of scripts. When is it better to do
var foo = function() { console.log("Foo!"); };
than
function foo() { console.log("Foo!"); }
when it's obvious that function name(){}
is more compact and seems to do the same things? At first, I thought it was so you could use foo
as a variable, but you can still do that with both methods. They can even both be redeclared by either method.
I know that var foo = function(){};
creates the function at runtime, but I don't see the advantage of this, because it can lead to crashes, which I assume are unwanted.