Welcome to the Treehouse Community
Looking to learn something new?
Why would i want to use an anonymous function over a named function?
Why would i want to use an anonymous function over a named function? It seems like the named function would do the job just aswel
Steven Parker216,012 Points
A named function would do the job as well, but sometimes you don't need a name.
For example, if you're passing a callback function and your coding the complete function in the argument of the other function you are passing it to. This is a very common practice seen in code all the time. It just doesn't need to have a name. And by not naming it, it cannot get accidentally overwritten later in the code.
These functions are written to be used once (like our callback functions).
If you know the function isn’t going to be reused, naming it or storing it to a variable doesn’t make sense. It would just add unnecessary lines of code to your project.
Andrew alludes to this in the first lesson of Callbacks with Timers.