Introduction to arrow functions in javascript

Spread the love

Arrow functions in JavaScript

Arrow function were introduced in ES6 version of JavaScript. These functions allows us to create functions in a better and concise way as compared to regular JavaScript functions.

How to use arrow functions in javascript

The syntax of arrow functions in JavaScript is as follows:

// function expression
let arrowFunction = (arg1, arg2, ... argn) => {
   statement(s)
}
  • The arrowFunction is the name of the method
  • The arg1, arg2, arg3 …. argn are the function parameters
  • Statement(s) is the function body

An example arrow function

Below is an example arrow function, that multiply 2 numbers and log the result.

let multiply = (x, y) => {
  return x * y;
};

console.log(multiply (10, 20)); //200

Shorter syntax for arrow function

If there is only one statement in the function then we can omit the braces {} and return keyword.

let multiply = (x, y) => x * y;

Arrow function with out parameters

You can create an arrow function without parameters as defined below.

let helloWorld = () => console.log('Hellow World!');

helloWorld();

Arrow function as an expression

The arrow function can be dynamically created as a function and used as an expression. The value of $marks is evaluated and the arrow function according to the result is executed.

let marks = 45;


let result = (marks > 45) ? () => console.log('Pass') : () => console.log('Fail');

Traditional anonymous function vs arrow function

If we compare the traditional anonymous function with the arrow function as given below.

/** An Anonymous Function */
(function (num) {
  return num * 2;
});

//Step -1:  Delete the function reserved keyword
(num) => {
  return num * 2;
};

// Step - 2. Delete the brackets and return reserved keyword
(num) => num * 2;

// Step - 3. Delete the parentheses around parameters
num => num * 2;

There are some important considerations while using arrow functions.

1. Use of new operator in arrow functions

You should not use new operator and create a constructor with arrow functions

let Sum = () => {};
let sum = new Sum(); 
// Error will be thrown: Sum is not a constructor

2. Using this in arrow functions

An important consideration while using arrow functions is use of this keyword. Also the arrow functions should not be used as methods inside objects.

let product = { 
   name: 'Mobile Phone', 
   price: 4000, 
   displayPrice: () => { 
     // Here this keyword refers to the global or outside this and will be undefined ..... 
     console.log(this.price); 
   } 
} 


product.displayPrice(); 

//undefined - The this.price expression will not work and undefined error will be thrown

In the above method the this.price does not work because this keyword does not belong to the arrow method and it refers to global scope.

3. Parameter binding for arrow and conventional functions

Parameter binding also does not work inside arrow functions as they work in conventional functions.

Suppose if you pass parameters to a function then these parameters can be accessed using arguments reserved keyword, but inside arrow functions, it will throws an error.

let checkArguments = function () {
    console.log(arguments);
}
checkArguments(15,30,45);

//The output is: Arguments { 0: 15, 1: 30, 2: 45 }

If we use arguments keyword inside arrow function, an error will be displayed.

let checkArguments = () => { 
     console.log(arguments); 
} 

checkArguments(15,30,45); 

//An Error will be displayed: Uncaught ReferenceError: arguments is not define

To work around for this error, you can use spread operator.

let checkArguments = (...args) => {
    console.log(args);
}

checkArguments(15,30,45);

//This will display output: Array(3) [ 15, 30, 45 ]

Summary

In this article, we have explored an important ES6 topic, arrow functions in JavaScript. Arrow function deceleration and limitations are discussed. If you want to read detailed tutorial, Please subscribe to our YouTube channel, follow us on our Facebook page, or follow us on twitter or GitHub.

Learn JavaScript

Related Articles

 

Previous Article:

 

Check Also

How to make a password generator in python

How to make a python password generator

Spread the loveLast updated:11th September, 2022How to make a python password generator Python is a …