I'm asking this because I just saw it on a piece of code:
var myVar = function func(arg){
console.log(arg);
}
I don't understand why function is "renamed" func just before it was defined for myVar.
Can somebody explain the interest of doing this, and not just:
var myVar = function(arg){
console.log(arg);
}
Thank you very much!
In your first example, you have a variable named myVar, which has a reference to a function named func. Your function isn't renamed.
In the secound example, though, you have the same variable myVar, but in this case, it's pointing to an anonymous function.
The reason for choosing number one over number two is that you get better output when errors occur, as it will print the function name. In the second example, it will simply say undefined if something goes wrong.
Edit: Found a more detailed answer here: Why use named function expressions?
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