What is the arrow function, and how to create it ?
Last Updated :
14 Apr, 2023
Function in any programming language is the basic building block to create and combine the related bits of code. Every programming language provides certain kinds of practices to write any function. The arrow function syntax is one of the most used and efficient ones to create a function in JavaScript.
How to create an arrow function: To write the arrow function, simply create any variable it can be const, let, or var but always do prefer const to avoid unnecessary problems. And then do assign the function code to the variable that's it. So from now, you can call that function by writing the parenthesis in front of that variable. With arrow function syntax, we consider a function as an object and assign the definition to some variable. Following is the syntax of the arrow function:
const myFunction = (param1, param2,
.... paramN) => { // function code }
const myFunction = (param) => { // function code }
or
const myFunction = param => { // function code }
const myFunction = param => { return param*param }
or
const myFunction = param => param*param
We can omit the {} parenthesis when there is only one statement and the javascript considers that statement as a return value, also there is no need to write parenthesis () when there is only one parameter. The arrow function can't contain the line break between the (params) and the arrow =>, Also there should not be any space between the = and > characters.
Example 1: In this example, we will create a program to add two numbers first using the normal function and then using the arrow function.
Using normal function:
JavaScript
function myFunction() {
let a = 5, b = 4;
return a + b;
}
console.log(myFunction());
Using arrow function:
JavaScript
const myFunction = () => {
let a = 5, b = 4;
return a + b;
}
console.log(myFunction());
Output: In the arrow function, we don't write the function keyword, so it is necessary to assign it to some kind of variable like here we have assigned to a constant variable named myFunction.
9
Example 2: In this example, we will create a function of single expressions using both normal and arrow functions one by one.
Using normal function:
JavaScript
function myFunction(num) {
return num * num;
}
console.log(myFunction(4));
Using arrow function:
JavaScript
const myFunction = num => num * num;
// Equivalent to const myFunction = (num) =>{ return num*num; }
console.log(myFunction(4));
Output: When we have only one return expression in the function, arrow syntax provides an easier way to write. We can drop the parenthesis of the parameter and also the return statement along with code blocks braces.
16
Limitations of Arrow Functions: Earlier we have seen how easily we can create a function with the arrow syntax now it's time to look at some limitations also because they will not work similarly to normal functions in several situations.
No binding of this keyword: It cannot be used as a method because of not having a binding of this keyword. The arrow function contains the lexical this instead of their own. The value of this will be based upon the scope in which they are defined. So the arrow function is defined in the window scope hence this will refer to the window object instead of the object in which the function is been written. There doesn't exist any property or method with 'num' hence undefined will be printed.
JavaScript
var obj = {
num: 10,
myFunc: () => { console.log(this.num) }
}
obj.myFunc();
Output:
undefined
Explanation: The normal function contains this which refers to the object to which it belongs, and hence the function belongs to the obj object and property num exists inside it, so will be printed successfully.
Arrow functions can't be used as constructors.
Example: This example shows that an arrow function can't be used as a constructor.
JavaScript
var MyFunction = () => { };
var instan = new MyFunction();
Output:

The call, apply, bind methods don't work with arrow functions: Below is the general recall for concepts of these methods with the example of the traditional function, and later we will see that it won't work well with arrow functions.
- call() Method: This method is used to call any function for an object, and inside the function, this will refer to the object being passed.
- apply() Method: It also works similarly to the call method. Instead of passing arguments separately here, we pass an array of arguments.
- bind() Method: This method is also used to call any function for an object but it creates another copy of that function by binding the object which can be used later.
Example: This example shows the use of the call(), apply(), and bind method in Javascript.
JavaScript
let studentObj1 = {
studentName: "Twinkle Sharma",
branch: "Computer Science & Engineering"
}
function showDetails(position) {
console.log(`${this.studentName} of ${this.branch} is a ${position}`)
}
showDetails.call(studentObj1, "Technical Writer");
showDetails.apply(studentObj1, ["Technical Writer"]);
const myFunction = showDetails.bind(studentObj1);
myFunction("Technical Writer");
Output: Because this refers to the scope in which the arrow function is defined means the window This is the reason why the call, apply, and bind methods don't work with the arrow function. Hence we can only access the window with the help of the arrow function. And as the properties student name and branch don't exist in the window object undefined will be printed.

Similar Reads
Can we use Hoisting with Arrow function ?
In this article we will see how does exactly Hoisting works using both normal function as well as arrow function in JavaScript. Arrow function: Arrow function introduced in ES6, provides a concise way to write functions in JavaScript. Another significant advantage it offers is the fact that it does
3 min read
How to Find the Inverse of a Function
In math, finding the inverse of a function means undoing what the original function does. The inverse switches the input and output of the original function. But not all functions have inverses; there are rules to for an inverse to exist for a function. This article looks at the steps to find a func
8 min read
How to Declare the Return Type of a Generic Arrow Function to be an Array ?
In TypeScript, declaring the return type of a generic arrow function to be an array is generally essential when we want to ensure that the function always returns the array of a particular type. This is especially necessary for maintaining type safety and maintaining consistency in our code. Declari
3 min read
What are Arrow/lambda functions in TypeScript ?
Arrow functions in TypeScript (also known as lambda functions) are concise and lightweight function expressions using the => syntax.Provide a shorter syntax for defining functions.Automatically bind the context of their surrounding scope.It is commonly used for callbacks, array methods, and simpl
3 min read
Explain the arrow function syntax in TypeScript
Arrow functions in TypeScript are implemented similarly to JavaScript (ES6). The main addition in TypeScript is the inclusion of data types or return types in the function syntax, along with the types for the arguments passed into the function.What is arrow function syntax in TypeScript?Arrow functi
3 min read
What Do Multiple Arrow Functions Mean in JavaScript?
In JavaScript, arrow functions provide a concise syntax for writing function expressions. When you use multiple arrow functions in sequence, it typically signifies a series of nested functions, often used for currying or higher-order functions. These are the following methods to show the multiple ar
2 min read
When should one use Arrow functions in ES6 ?
In this article, we will try to understand when should one use Arrow functions in ES6 instead of the traditional function's syntax with the help of some examples. Before analyzing why we should use arrow functions, let us first understand the basic details which are associated with the arrow functio
4 min read
What is a typical use case for anonymous functions in JavaScript ?
In this article, we will try to understand what exactly an Anonymous function is, and how we could declare it using the syntax provided in JavaScript further we will see some examples (use-cases) where we can use anonymous functions to get our results in the console. Before proceeding with the examp
4 min read
Functions that Describe Situations
Functions in mathematics are used in the description of the relationship between one or more variables. Thus, functions can help understand how one quantity depends on another and, therefore, model various practical situations. This article should introduce the different types of functions and analy
7 min read
What is JavaScript >>> Operator and how to use it ?
The JavaScript >>> represents the zero-fill right shift operator. It is also called the unsigned right-bit shift operator. It comes under the category of Bitwise operators. Bitwise operators treat operands as 32-bit integer numbers and operate on their binary representation. Zero-fill right
3 min read