A Brief Javascript Arrow Function Explanation

When starting with Javascript, arrow functions were something that took me awhile to understand. Hopefully this brief explanation will help others understand what seemed to take me forever to grasp.

Starting with a named function with two parameters as seen below.

I like to think of the “arrow” as replacing the word function. The return keyword is no longer needed because if everything is in one line it is assumed to be returned. Both of these functions above will produce the same results but the latter is much easier to use (once you understand how it works).

We can do the same with another named function with one parameter as seen below.

In this example there is something else to note. When there is only one parameter it is not necessary to put parenthesis around that parameter. Again the return is implied because it is written on one line. Both functions will work exactly the same but the arrow function is easier to read once you get used to seeing them.

Finally an example of a named function and an arrow function with zero parameters.

Both functions once again will work as intended. If the function does not require a parameter though you will need to include empty parenthesis.

I hope this short blog helped you to understand arrow functions. Happy coding!!!