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

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 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.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 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.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 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.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 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.

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.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 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.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

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...

5 minutes read.

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 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.

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.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 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.