An introductory guide to ES6 arrow functions in JavaScript


Notice: Trying to access array offset on value of type null in /bitnami/wordpress/wp-content/themes/echologyx/single-post.php on line 25

Notice: Trying to access array offset on value of type null in /bitnami/wordpress/wp-content/themes/echologyx/single-post.php on line 25

What are the benefits of arrow functions in JavaScript?

  • They are more concise.
  • They have implicit returns. (We’ll get into this below.)
  • They do not rebind the value of this when you use an arrow function inside another function. (We’ll get into this in a later post.)

How do I convert my ES5 functions to ES6 arrow functions?

Let’s start with a simple example using .map().

// Let's define an array of first names:
const names = ['joe', 'rache', 'micaela'];
// If I want to add my last name to each I'll run the following function using .map():
const fullNames = names.map(function(name) {
return `${name} cardillo`;
});
// In the console when I call:
names
// It returns:
['joe', 'rache', 'micaela']
// Then if I call the function:
fullNames
// It returns:
["joe cardillo", "rache cardillo", "micaela cardillo"]

To start, delete the word ‘function’ and replace it with a fat arrow =>

const fullNames = names.map((name) => {
return `${name} cardillo`;
});

If you have only one parameter you can remove the parenthesis from it:

const fullNames = names.map(name => {
return `${name} cardillo`;
});

You can also do an “implicit return.”

But first, what is an explicit return?

An explicit return is when you explicitly write the word return in the function.

To do an implicit return, remove the word return, then move what you are returning up to the same line as the rest of the function. You can remove the curly brackets.

// Explicit return:
const fullNames = names.map(name => {
return `${name} cardillo`;
});
// Implicit return:
const fullNames = names.map(name => `${name} cardillo`);

If there is no parameter you can replace it with parenthesis:

const fullNames = names.map(() => `hey cardillo!`);
// Returns:
["hey cardillo!", "hey cardillo!", "hey cardillo!"]

Arrow functions are always anonymous functions.

Before describing an anonymous function, let’s first answer the question, “What is a named function?”

// In ES5 a named function can be written as follows:
function myFavFood(food) {
console.log(`My favorite food is ${food}!`);
}
// myFavFood is the name of the function.
// Calling this in the console:
myFavFood('pizza');
// Returns:
My favorite food is pizza!

In the above arrow functions, notice that we set all of them equal to a constvariable called fullNames.

So why would we want to set our arrow function equal to a variable?

One reason is that it can help us debug JavaScript errors, since sometimes the line number isn’t enough to narrow down where it’s coming from.

Using the food example, we can assign the arrow function to a const variable called myFavFood.

const myFavFood = (food) => { console.log(`My favorite food is ${food}!`) }
// In the console:
myFavFood('pizza');
// Returns:
My favorite food is pizza!

Don’t just take our word for it…

Don’t just take our word for it…

Customers’ Talking

Our customers are our biggest advocates. That’s why we let them do the talking.

Initially, when working with EchoLogyx, we ran a few test experiments to see how they perform. They did really well, QA was really positive. And then we sort of stepped it up very quickly from there. We are producing 12-15 tests a month. Communication is absolutely great. I can contact the team at any point in the day even after hours they are still responding to me. Since working with EchoLogyx, we have never had an issue with a test that has gone live.

Let's Have a chat?

If you want to find out a bit more, just get in touch. We love a chinwag, and we'd love to help you out.

Contact Us