Arrow Function in ES6

Arrow Function in ES6

 An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function allows us to write short syntax of the function.

The Arrow function also makes the code structured and easy to read. This function can be used as an anonymous (a function without having a name) function. It does not return any statement and also can be declared without using a function keyword. 

The developers can also use the arrow function as a constructor. The context of an arrow function is defined in a static and lexical manner. In various languages, the arrow function is also called the Lambda function.  The arrow function does not contain prototype property, and we cannot use it with any new keyword.

Syntax

const function_name = (args1, args2) = >
{
         // Code statement to be executed
} 

The syntax of Arrow function contains the following three parts-

  • Parameters: The arrow functions may or may not have the parameters.
  • The Fat Arrow Function (=>) / Lambda Notation: It is the notation of the arrow function.
  • Statement: It represents the group of instructions of the function.

We will try to understand it with the help of an example.

Example

var mygen1 = function display ()                            // Function Expression
{
         console.log (“Function Expression”);
}
var mygen2 = function ()                                       // Anonymous Function
{
          console.log (“Anonymous Function”);
}
var mygen3 = () =>                                                  // Arrow Function
{
          console.log (“Arrow Function”);
}
mygen1 ();
mygen2 ();
mygen3 (); 

In the above example we have defined three functions; Function Expression, Anonymous Function, Arrow Function.

Output

After execution of the code, we got the following output:

There are also some syntactic variations of Arrow function given below-

  1. For the single parameter, the parentheses are optional

For Example

var value = a =>
{
          Console.log (a);
}
value (360); 

Output

After the execution of the example, we got the following output:

  • For the single statement the optional and empty braces (If there is no parameter)

For Example

var display = () => console.log (“Welcome to ES6”);
display (); 

Output

After the execution of the code, we got the following Output:

Arrow function with Parameters

In the ECMAScript program, if we need to pass multiple parameters, then we pass them within the parentheses.

Example

var display (p, q, r) =>
{
          console.log (p + “ ” +q “ ” +r);      
}
display (150, 250, 350); 

Output

Arrow Function in ECS6

The Arrow function with default parameters

ECMAScript provides a facility to initialize a parameter with default values if the value is undefined or null.

Here, we have an example to understand it better.

Example

 var display (r, s = 100) =>
{
       console.log (r + “ ” + s);
}
display (50); 

Output

After the execution of the code, we got following result.

Arrow Function in ECS6

Arrow function with rest parameter

The rest parameter allows us to pass a number of values in the function having the same type. The rest parameter also works like a procurator for various arguments that have the same type.

We can declare the rest parameter; the name of the parameter must be prefixed with the spread operator. Let’s try to understand it with the help of the following example.

Example

var display (p, …args) =>
{
          console.log (p + “ ” +args);
}
display (10, 20, 30, 40, 50, 60); 

Output

After the successful execution of the code, we got the following output:

Arrow Function in ECS6

Arrow function without parentheses

We can define an arrow function without parentheses if we have only a single parameter to pass. It means parentheses are optional.

Example

var display = u =>
{       
          console.log (u);
}
display (“Welcome to programming world”); 

Output

After the execution of the example, we got the following output:

Arrow Function in ECS6

Advantages of Arrow function

There are the following advantages of ECMAScript 6.

The arrow function is used to wrap the content statically or in a lexical manner. It is difficult to handle this keyword in an arrow function as compared to normal function. The keyword this is not wrapped in an arrow function. The keyword this is used to show an object in regular function. The object should be a window, a document, a button, or any other. In the arrow function, the keyword this is used to present the object that determines the arrow function.

Example

In ECMAScript 6 – With the arrow function

this.value.forEach((value) =>
{
          if (value < 20)
this.parent.push (value);
}); 

In the above code, we have a class with an array of values. If the value is less than 20, then it should be pushed into the parent queue.

Example

In ECMAScript 5

this.value.forEach(function(value)
{
          if (value < 20)
this.parent.push (value);
}.bind(this)); 

The syntax arrow function always helps us to write less program code. It means it allows decreasing the quantity of the code.

The return statement and the function braces are optional for a single line function. In ECMAScript 5, it is mandatory to define the return statement inside the function, but in ECMAScript 6, there is no need to determine the return statement for the single line function.

Example

In ECMAScript 6

var display = num = > num/2;
console.log (display (500)); 

Example

In ECMAScript 5

function display (num)
{
          return num/2;
} 

Important Point: If we do not use curly braces with a single statement, then there is no need to use the return statement, but if we use then we must have to use the return keyword.

We will try to understand it with the help of an example.

Example

With return statement

var display = number =>
{
          return number/2;
}
console.log (display(300));       

Output

After the execution of the code, we got the following output:

Arrow Function in ECS6

Without return statement

var display = number =>
{
          number/2;
}
console.log (display(300)); 

Output

After the successful execution of the example, we got the following output:

Arrow Function in ECS6

Related Topics

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.