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'); };    

holaEs6();
 

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 = fruits.map(function(fruit) {      
return fruits.price;  });    

console.log(prices); // [1, 2, 3] 
   
// ES6  const prices = fruits.map(fruit => 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) {              
   resolve(data);          
   });      
  });  
};    

// ES6

API.prototype.get = function(param) {      
     return new Promise((resolve, reject) => {          
       http.get(this.uri + param, function(data) {              
         resolve(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 : 1021  
 
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 END DEVELOPMENT.

 
 
 

Related Posts

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…

How to reverse an array in JavaScript

In this tutorial we are going to see how you can change the order of the elements of an array so that they are inverted. You could use a loop…

How synchronize the scroll of two divs with JavaScript

In case you have two divs of different sizes you may sometimes want to scroll both at the same time but at different speeds depending on their size. For example,…

How to use the codePointAt method in JavaScript

The JavaScript codePointAt method has more or less the same function as the charCodeAt method, used to get the 16-bit Unicode representation of the character at a certain position in…

How to check if a value is a number in JavaScript

In this short tutorial we are going to look at the various methods that exist to find out if a value is a number in JavaScript.   1. Using the isNaN() function   One…

How to use the charCodeAt method in JavaScript

The charCodeAt method is accepted by strings in JavaScript, returning the 16-bit Unicode code of the character at the position we pass as a parameter to the method. The charCodeAt method…

How to use the charAt method in JavaScript

The charAt method is accepted by strings in JavaScript, returning the position of the character passed as a parameter within the string. If the string contains multiple occurrences of the character…

Strings in JavaScript: What they are and how to use them

In this tutorial we are going to explain what strings are and how they are used in JavaScript. The tutorial is intended for people who are learning to program in…

Dates in local format with Javascript

In the articles we have about dates in JavaScript we were missing one about how to create dates in local format with JavaScript. That is to say, being able to…

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