Arrow functions in JavaScript (=>)
Tags: javascript, es6, webdev, programming
Published: 2022-03-09 11:24:27

The humble function has existed in JavaScript since the start. But despite its wonderfully simple and near-impossible-to-screw-up design, it is often replaced in newer libraries and frameworks by something else: **the arrow function**.

Arrow functions are a simple, compact form of function that are better suited for situations where writing less lines are important, and a low character count is vital. My number one example for such cases are for array functions, but we'll come back to that.

To start with, here's a traditional JavaScript function. It takes a parameter, and says hi to said parameter:

{% embed https://gist.github.com/DuncanMcArdle/e63d174fa44dfd9e156b1a9925ad3fd6 %}

Now, converting a traditional function to an arrow function is pretty straightforward, but one trick for making it even easier, is to first change your traditional function to a variable function, like so:

{% embed https://gist.github.com/DuncanMcArdle/9b322d9299a8814013b61f13de3af820 %}

Next, we simply take away the `function` keyword, add an arrow (which is simply an equals symbol and a right chevron: =>) after the parameter(s), and we have an arrow function:

{% embed https://gist.github.com/DuncanMcArdle/3166edf6ab18f2f81028751024128517 %}

Yes really, it's that simple. Not particularly useful at this point, but simple.

Now let's start to cut things down a bit. First off, we can shorten the body of the function dramatically by removing the braces (**note:** this is possible only if your arrow function contains a single statement, as otherwise the end of your arrow function is ambiguous):

{% embed https://gist.github.com/DuncanMcArdle/9af7742ad72959a735a3c45c6b45cb0e %}

You can even trim off a couple more characters **if you only have a single parameter**, by removing its surrounding parenthesis:

{% embed https://gist.github.com/DuncanMcArdle/bf2badf6e7bd8bb71e16afe2dc38c375 %}

However, **I strongly discourage doing this**. If you use multiple parameters, or if you have no parameters at all, parenthesis will be required, such as in the following two examples:

{% embed https://gist.github.com/DuncanMcArdle/92e8523f7c3db29d5ef194bcd78232d9 %}

So my personal preference (and the one enforced by many code formatting tools like Prettier) is to always use parenthesis. **Consistency is key in coding**.

Now that you have a grasp of arrow functions, you might be thinking to yourself, "Well great, but where would I want to use one?". The answer to that is: lots of places! But I'll highlight one important one: array functions.

As you're no doubt aware, JavaScript has an assortment of array functions. Some sort, some chop, some add, and some analyse the contents of an array, amongst other things. One thing that is common with these functions though, is that they often take a function as a parameter. Here's an example that takes an array of objects, and sorts them by a parameter:

{% embed https://gist.github.com/DuncanMcArdle/84ef9aa0b508ccad2fb84d54bdd427c0 %}

A little clunky right? Well, let's cut things down a little by defining the function inside of the sort:

{% embed https://gist.github.com/DuncanMcArdle/9efda3632c633a56819c125d5f58eb4f %}

Better, but not yet perfect. Let's convert things to an arrow function (**note:** we're omitting a return keyword here, which is totally fine here as the return value is obvious, but not with bigger arrow functions where the returned value is ambiguous, such as when multiple statements are carried out):

{% embed https://gist.github.com/DuncanMcArdle/605836b3587c0412f859669427e1df40 %}

Perfect! It's concise, readable, efficient, all the things we're looking for when doing CPU intensive things like sorting large arrays.

Now, it's worth noting that this is a very simplistic example. The real benefit of arrow functions comes when dealing with bigger, more complex use cases, where a small reduction in character count can have a massive impact both on readability and on performance.

I'd also like to mention that arrow functions are not a direct replacement for traditional functions. They differ in how they use `this`, don't have access to arguments, and can't be used as methods, amongst other things. Have a read of [this great Mozilla page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) and make sure you fully understand them, and only use them where appropriate.