tutorialstonight logo


What is arrow function in JavaScript?

Arrow function is one of the features introduced in ES6 for writing better javascript.

Arrow function in javascript is an alternative way to write a function with shorter and cleaner syntax than a regular function.

An arrow function is also called fat arrow function.

arrow function in javascript

Syntax of arrow function

() => { ... } // no parameter
arg1 => { ... } // single parameter
(arg1, arg2, arg3, ...) => { ... } // multiple parameter

Below is an example of arrow function which takes 3 argument and returns the sum of the arguments.

▶ Try It

In the above example 'add' is name of the function, parenthesis holds arguments and curly braces contains statement of the function.

In arrow function when there is only one statement then You can omit curly braces and just write the statement.

▶ Try It

When there are multiple statements in the arrow function then you can't omit curly braces. Here is an example of an arrow function which has multiple statements.

▶ Try It

Javascript Arrow Function Without return Keyword

Now you have a basic understanding of how to write arrow function, let's explore it more.

Writing arrow function with single statement is even shorter with a javascript arrow function.

In the above example, a + b is returned using return keyword, but when there is an only single statement in arrow function then you can omit return keyword and remove curly braces ( {} ) and it still works in the same way.

Arrow function return value by default.

▶ Try It

Javascript Arrow Function With Single Argument

Arguments are wrapped up in small bracket () in any kind of function, but when there is only 1 argument in arrow function then you can omit parenthesis and write the only argument.

In the example below there is only one argument 'name' for the 'greeting function'.

▶ Try It

Note: Removing parenthesis only works when there is only 1 argument.

Javascript Arrow Function With No Argument

When there is no argument in arrow function then you can write arrow function in 2 different ways. First, simply use parenthesis () before the arrow (=>) with no argument, and second, you can use underscore ( _ ) replacing parenthesis.

▶ Try It

Let's see another example which has underscore _ in the place of parenthesis.

▶ Try It

Javascript Arrow Function VS Function

An arrow function is very much different from a regular function. At some places, it is better to use arrow function while at different place regular function is better.

Let's see the difference between arrow function and regular function for better understanding.

Arrow Function Regular Function
Syntax Syntax of arrow function is different from regular function, arrow function use ( => ) to create function The regular function does not use the arrow to create a function
Return Arrow function has implicit return, you need not use the return keyword to return in the function Regular function need return keyword to return
Duplicate parameters Duplicate parameters are not allowed in an arrow function When not using 'use strict' you can have duplicate parameters
Constructor An arrow function is not constructible, which means you can't use new to construct The regular function can use the new keyword to construct
value of this this in arrow function remains same, no matter from where and who invoked function In regular function, this keyword depends on how and where the function is invoked

this In Arrow Function Javascript

Javascript arrow function handles this differently.

In regular function, this represents the object that calls the function, like button, document, window etc but this in arrow function represents only the object that defines the arrow function.

Let's see an example to understand the difference.

▶ Try It

The output is "undefined runs at undefined". You got undefined because the callback function set in setTimeout for invokation is regular function and this mean that this for the function is set to window object, because this in regular function depends on who invoked the function.

So if some how we could have referred this to the same object which defined the function then we can solve the problem.

This is what an arrow function can do. The this of arrow function refer to the object which defines it. See the example below to understand.

▶ Try It