The following is probably a bit senseless, but why does the first call below work while the second one fails?
var foo = function bar() {
console.log("Martini");
}
foo(); // works
bar(); // undefined; i.e. "Uncaught ReferenceError: bar is not defined"
Has that to do with scope?
Corollary beginner's question: The function definition "parses" - but is that actually valid syntax - and is there any context where naming an assigned anonymous function makes sense?
Function declarations create a variable with the same name as them in the current scope.
Function expressions (named or anonymous) do not. The name of a function expression is available as a variable inside the function (where it is useful for calling itself recursively) and in debugging tools.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With