The JavaScript arrow function is a shorter way of writing a function expression that was introduced in ECMAScript 6. Usually in JavaScript, you can create a function in two ways:
- Function as statement
- Function as expression
A function statement can be created as shown below:
The same function can be created as a function expression as shown below:
ECMA 2015 (or ECMA Script 6) introduced shorter syntax to write a function expression called the arrow function. Using the arrow function, you can write the function expression above as seen here:
As you see, writing a function expression using the arrow function is shorter.
Basic syntax rules for arrow functions
First, parameters should be passed in the small parentheses. You can create an arrow function with two parameters as shown below:
Leave a Reply