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 = {
return `${name} cardillo`;
// In the console when I call:
// It returns:
['joe', 'rache', 'micaela']
// Then if I call the function:
// It returns:
["joe cardillo", "rache cardillo", "micaela cardillo"]

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

const fullNames = => {
return `${name} cardillo`;

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

const fullNames = => {
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 = => {
return `${name} cardillo`;
// Implicit return:
const fullNames = => `${name} cardillo`);

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

const fullNames = => `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:
// 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:
// 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.

  • Working with EchoLogyx has simply changed the development lifecycle of our website. Their experienced A/B testing developers and QA engineers have significantly increased our efficiency.

    Farhan Hussain

    eCommerce Director, eSpares
    4 3 2 1 0
  • I’d 100% recommend EchoLogyx – they have really sort of raised the bar of my expectation, of how you should work with developers and also their efficiency of completing the project.

    Lucy Collins

    UX Consultant, Web Usability
    4 3 2 1 0
  • 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 Al Jannati

    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

Leaving so soon?

Get 20% OFF on your first project

When you Sign up to our email newsletter

You will receive:
  • Top tips on A/B testing development and QA
  • Testing tools and their performance
  • QA and Bug Free testing

Contact us

Why don't you get in touch to find out more about our services? Fill in the form below: