JavaScript Arrow functions: What they are and how to use them

by Janeth Kent Date: 30-08-2021 javascript

In this article we are going to see what they are and how to use JavaScript Arrow Functions, a new feature introduced with the ES6 standard (ECMAScript 6).

What are Arrow Functions

Arrow Functions are a new syntax for defining anonymous functions in JavaScript in a more concise way. Being an abbreviated syntax, it will save us a lot of time, as well as simplifying the function scope. It is one of the most widely used features of ES6.

Currently, most browsers already support Arrow Functions, so we can use them safely. However, they are not supported by any version of Internet Explorer.

To define an Arrow Function we use the symbol "=>", whose shape reminds us of a double arrow. It is not necessary to write neither the word function nor the function opening and closing braces, being a case similar to the Lambdas of languages like Python or C#. Arrow functions can be defined in a single line.

Arrow Functions Syntax

We can define arrow functions in many ways. In this section we are going to see the most used ones, as well as the differences between an ES6 Arrow Function and a normal function.

Let's define a very simple example function that adds two numbers.

// ES5  var sumaEs5 = function(x, y) {      
     return x + y;  
// ES6  const sumaEs6 = (x, y) => { return x + y };   
console.log(sumaEs6(2, 4)); // 6

As we can see, with the arrow function we save the word function, and we can also write the function in a single line maintaining the recommended good practices.


Syntax with one parameter


In the case of using a single parameter, we can omit the parentheses surrounding the parameter. As an example, a function that calculates the square of a number:

// ES5  var cuadradoEs5 = function(x) {      
     return x * x;  };    

// ES6  
const cuadradoEs6 = (x) => { return x * x };  
console.log(cuadradoEs6(3)); // 9

Syntax without parameters


In the case of not using any parameter, we will have to write the parentheses anyway. As an example, a function that makes an alert:

// ES5  
var holaEs5 = function() {     
    alert('Hola');  };  
// ES6  
const holaEs6 = () => { alert('Hola'); };    


Literal expression syntax


Arrow functions can also return an object in its literal expression form. Objects are defined between braces, something that would conflict with the definition of arrow functions. That is why to return a literal we will have to surround the function with parentheses. That is to say, in addition to the braces, we will have to place an opening and a closing parenthesis. As an example, this function that transforms two parameters into an object:

// ES5  var agregarIdEs5 = function(id, nombre) {      
    return {          
       id: id,          
       nombre: nombre      

// ES6  
const agregarIdEs6 = (id, nombre) => ({ id: id, nombre: nombre });   
console.log(agregarIdEs6(1, "Edu")); // Object {id: 1, nombre: "Edu"}

Now you know how Arrow Functions are defined, so we will see how to use them. We will also see some practical examples.


How to use Arrow Functions


We are going to see a series of examples in which you will be able to clearly see the advantages of the Arrow Functions.


Array Manipulation


A very extended use case of the Arrow Functions is the manipulation of arrays. In this example we are going to define an array of cars with their name and price. Then, we will create a function to extract the prices of all the cars with both ES5 and ES6:

const fruits = [      
{ name:'Apple', price:1 },      
{ name:'Banana', price:2 },      
{ name:'Peach', price:3 }  
// ES5  var prices = {      
return fruits.price;  });    

console.log(prices); // [1, 2, 3] 
// ES6  const prices = => fruit.price); 
console.log(prices); // [1, 2, 3] 

As we have seen, the second function is shorter and more concise, being also easier to read.

Let's look at another example where we filter the numbers in an array that are divisible by three.

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];    
// ES5  var divisiblebyFour ES5 = array.filter(function (n){      
return n % 4 === 0;  }); 
// ES6  
const divisiblebyFour ES6 = array.filter(n => n % 4 === 0);   
console.log(divisiblebyFour ES6); // [4, 8, 12, 16, 20]

Again, the ES6 version is shorter and more concise. Let's look at another typical use case in the next section.


Promises & Callbacks

When we use callbacks in asynchronous functions, we can find a lot of functions and values that are returned or passed to other functions. If we use Promises, these expressions will be concatenated to each other as you can see in the following example where we call three asynchronous functions, one after the other. The code becomes simpler if we use Arrow Functions

// ES5  
aAsync().then(function() {      
retbAsync();  }).then(function() {      
retcAsync();  }).done(function() {      
finish();  });  
// ES6  
aAsync().then(() => bAsync()).then(() => cAsync()).done(() => finish);

In addition to benefiting from a shorter syntax, we eradicate the confusion of using the word this. One of the problems with calling functions this way in ES5 is that we will have to pass the context somehow to the next function.

As we said before, when we use Arrow Functions the context is kept between functions, so let's see the differences with an example:

// ES5

API.prototype.get = function(param) {      
   var self = this;      
   return new Promise(function(resolve, reject) {         
   http.get(self.uri + param, function(data) {              

// ES6

API.prototype.get = function(param) {      
     return new Promise((resolve, reject) => {          
       http.get(this.uri + param, function(data) {              

In ES5 we have to use several techniques to obtain the context the this, like this famous clause:

var self = this;.

By means of this clause or closure, we obtain the context of the parent function in ES5. On the other hand, with the ES6 Arrow function, the context of the parent function is maintained. Another method also widely used in ES5 is to use the .bind method, which in practice proves to be quite slow.


Considerations about Arrow Functions


Arrow Functions, like everything else, have their advantages and disadvantages. In this section we will see what they are.

To begin with, the syntax of the functions can be more confusing for some people and even make it more difficult to read, as it is less natural. At least, this is what some people think, but the truth is that there are more developers in favor of its use than against it.

So that you don't hate Arrow Functions because of multiple errors that you might mistakenly think are bugs, there are several things to keep in mind:

This: The word this works in a different way in Arrow Functions. The value of this cannot be modified inside the function and, even if you try, you will find that its value is the same as the one with which the function was called. The call, apply and bind methods will not alter its value either.

Constructors: Arrow functions cannot be used as constructors in the same way as other functions. If you use the reserved word new with an Arrow function, you will get an error. These functions do not have a prototype property or other internal methods. Constructors are usually used to clear objects of a class, so it is best to use the classes that have also been incorporated in ES6.

Generators: Arrow functions cannot be used as generators, which are functions defined with function* syntax.

Arguments: Arrow Functions do not have an internal variable defined as arguments as other functions do. The arguments variable is an array that allows us to obtain the arguments of a function, something useful in case a function has an indeterminate number of arguments. Remember that this functionality is not available in the Arrow Functions.

There are a few more, but these have been the top four considerations you should keep in mind.


When to use an arrow function


When and where to use Arrow functions is up to you, but here are three recommendations.

Use the classic functions - function - in the global scope and for properties defined as Object.prototype.

For the rest of the functions, except when you define a constructor, which you should use class, you can use arrow functions as long as their particular characteristics do not prevent you from creating them. In fact, it is to be expected that, as with let and const, this method will be the standard method for defining a function.

by Janeth Kent Date: 30-08-2021 javascript hits : 3343  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.


Related Posts

How to use the endsWith method in JavaScript

In this short tutorial, we are going to see what the endsWith method, introduced in JavaScript ES6, is and how it is used with strings in JavaScript. The endsWith method is…

What are javascript symbols and how can they help you?

Symbols are a new primitive value introduced by ES6. Their purpose is to provide us unique identifiers. In this article, we tell you how they work, in which way they…

Callbacks in JavaScript

Callback functions are the same old JavaScript functions. They have no special syntax, as they are simply functions that are passed as an argument to another function. The function that receives…

How to create PDF with JavaScript and jsPDF

Creating dynamic PDF files directly in the browser is possible thanks to the jsPDF JavaScript library. In the last part of this article we have prepared a practical tutorial where I…

How to make your own custom cursor for your website

When I started browsing different and original websites to learn from them, one of the first things that caught my attention was that some of them had their own cursors,…

Node.js and npm: introductory tutorial

In this tutorial we will see how to install and use both Node.js and the npm package manager. In addition, we will also create a small sample application. If you…

How to connect to MySQL with Node.js

Let's see how you can connect to a MySQL database using Node.js, the popular JavaScript runtime environment. Before we start, it is important to note that you must have Node.js installed…

JavaScript Programming Styles: Best Practices

When programming with JavaScript there are certain conventions that you should apply, especially when working in a team environment. In fact, it is common to have meetings to discuss standards…

Difference between arrow and normal functions in JavaScript

In this tutorial we are going to see how arrow functions differ from normal JavaScript functions. We will also see when you should use one and when you should use…

How to insert an element into an array with JavaScript

In this brief tutorial you will learn how to insert one or more elements into an array with JavaScript. For this we will use the splice function. The splice function will not…

What is the difference between primitives types and objects in JavaScript?

In this short tutorial we are going to look at the differences between primitive types and objects in JavaScript. To start with, we're going to look at what primitive types…

How to get DOM elements with JavaScript

When you access any element of the DOM, it is usual to save it in a variable. This is something that at first might seem very simple, but if you…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration