An introductory guide to ES6 arrow functions in JavaScript

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!

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

Don’t just take our word for it…

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

  • They come to me saying, this can be done better. This proactiveness is what I most like about the EchoLogyx team. They have got a strong QA team as well so that nothing goes out live without going through a thorough QA

    Nabil AlJannati

    Brand Manager, Ninety Percent
    4 3 2 1 0
  • Our experience of Echologyx is resoundingly positive. They’re very thorough and detailed. We’ve got a great working relationship with our optimisation managers.

    James Amborse

    Head of Operations, RedEye Optimisation
    4 3 2 1 0