Why would I do function() and not function blah()?

Today I got asked the question:

In JavaScript (the best language) what is the difference between these two:

The first is an anonymous function.

The second is a named function.

Anonymous functions are one time deals.

Named functions can be used as many times as needed. Even zero times.

An example of an anonymous function:

The function (n) => n + 1 is anonymous. It has no name.

But you could define it as a named function:

And then you could repeat the map with the named function:

And likewise you could use the function again and again:

Even if your function is a one-and-done deal, naming functions can help you better explain your code to others (or yourself 5 minutes from now).

As soon as you name an anonymous function, it is no longer anonymous.

And that’s all I have to say about that.

Shares

Published by

Code Review

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.