How to use arrow functions in JavaScript.

Frontend Engineer who loves building tools.
Search for a command to run...

Frontend Engineer who loves building tools.
No comments yet. Be the first to comment.
Performance is a touchy subject in software engineering. It’s one thing to write code and ship it; it’s another thing entirely to make it faster and more enjoyable for your users — and the next developers after you. In this article, we will look at h...
Animations seem to be a necessity these days in frontend development, improving the user experience in apps by a significant margin. Thankfully vue.js shipped two built-in components that abstract away the complexities of animating our apps; transiti...
Arrays have a lot of helpful higher-order functions that make working with them easier. Our focus in this article will be the sort() method, this method has both easy and complex use cases which we'll be looking at in this article. First look at the ...
Gridsome is a static site generator used for building powerful frontend applications, it was built on the Vue.js framework. One of the beauties of Gridsome apart from its ability to generate server-side rendered applications is its compatibility with...
Build hooks are unique URLs used to trigger a build on the Netlify. They can be used to refresh the content on your website after you’ve published or unpublished content in your Storyblok space. Getting the hook URL from Netlify To create one, open y...

The arrow function is a cool addition to the ES6 syntax of writing JavaScript, introduced in 2015, arrow functions are a shorter way to write functions in JavaScript. Using an arrow function strips away the need to use the function keyword when initializing a function. The code snippet below is an example of an arrow function in comparison with the regular ES5 function. It's worth mentioning that arrow functions are completely anonymous (you cannot name them as you would with the regular ES5 syntax).
// cool new ES6 syntax
const greet = name => {
return 'Hello ' + name;
};
greet('Joe'); // Hello Joe
// old ES5 syntax
function greet(name) {
return 'Hello ' + name;
}
greet('Janet'); // Hello Janet
Notice how we had to assign the arrow function to a variable called greet? that's what I meant when I said:
It's worth mentioning that arrow functions are completely anonymous.
I know what you're thinking, we used the same four lines of code to write the arrow function so it doesn't make any difference? well, what if I told you that we don't need the return statement, the curly braces or even the parentheses in our first function declaration?
Observe:
const greet = name => 'Hello' + name;
greet('Joe'); // Hello Joe
wait, what just happened?
greet function, here's how we would do it:const greet = (firstName, surName) => 'Hello ' + firstName + ' ' + surName;
greet('Tony', 'Stark'); // Hello Tony Stark
=>, so the only time we'd use a curly brace and the return statement is when our arrow function has some other task to perform on the next line.Sadly, an arrow function cannot replace the regular function because it does not have the same use case as the regular function.
this, arguments, super or new.target hence it's unsuitable for use as a constructor.this keyword, arrow function methods that are going to return a value based on some calculations made from the values of other properties of the object will most likely yield unexpected results..map(), .sort() etc.this keyword 😅. Although it's fine to use the this keyword in arrow functions, as long as you know what you're doing.