JavaScript Arrow Function


Arrow functions look very different from the function's expression. Instead of the function keyword, it uses an arrow ( => ) made up of an equal sign and a greater-than character. The function expression and arrow function do the same thing apart from a minor detail, which we will discuss now. The arrow function provides an alternative way to write a shorter syntax compared to the function expression. The below example defines function expression.

The below example uses an arrow function that computes similar to the above function.

There are multiple ways we can use the arrow function. We can use it with the block and without the block. If you use te block syntax, you need to specify the return keyword:

The following expression

is equivalent to the below expression:

For two or more parameters we can use the following syntax:

Conclusion

Arrow functions are an interesting new feature in ECMAScript 6, and one of the features that are pretty solidified at this point in time. As passing functions as arguments have become more popular, having a the concise syntax for defining these functions.