Arrow Functions Are Not Always Needed
What are Arrow Functions
I think that this states its a COMPACT ALTERNATIVE which means at times this is the way to go and others not so much. In a section below I will speak of the pros and cons of using arrow functions all the time.
Now the example above is perfect and yes that could have been a let or a const but still. In this case, it is on a single line and easy peasy to read. That is the thing for me I am getting used to using them and really like it but when reading other code it can get intense.
When to use Arrow Functions
With what is stated above it is important to know that things are created for a reason. Arrow Functions can create clean code and some codebases may even enforce you to use them at times. If a function is being applied over and over again then the use of an arrow function is going to be your way to go.
For example, if you have an array of values that you want to transform using a map, an arrow function is ideal:
const stuff = ['bike', 'bread', 'shoes']; const downcasedStuff = stuff.map(stuff => stuff.toLowerCase());
Another place arrow functions make for cleaner and more intuitive code is in managing asynchronous code. To manage this you will need to have promises which will need to functions defined which is an ideal location for an arrow function.
These are just a few of the places where some arrow functions can come in handy. Don’t get me wrong my frustration is not being able to read them……A personal problem yes.
Cons of Too Many Arrow Functions
Not using them is going to come into play with objects and methods because if you do not have the entire thing mapped out then you will run into problems with testing and readability as well. Also if you are chaining function calls. For example, if you have anonymous functions that are repeated called finding them with no definition can turn into a nightmare.
Well, the reality is that Arrow Functions are actually pretty awesome and useful it done right. My examples of when not to use them may not even be 100% on. I think that using them wisely to save time is smart, and the better I get at reading them and writing them I will be a bigger fan. All in all, as I wrote this I learned why to use them and when not to.