Arrow function in javaScript

Arrow function in javaScript

When ES6 (ECMAScript 2015 or ECMAScript 6) came into the role, It was a major revision in javaScript and introduces new syntax.

One of these syntaxes was Arrow Function. Arrow function syntax introduces new ways to write a function, The way made code less and easy to write and readable too, we don't need to write function and return keywords for a single expression.

Without using Arrow function

code.png

Using Arrow function

code.png

Now let's see what's happens with a single line of code in Arrow Function.

code.png

code.png

So, The differences in the Arrow function are?

  • There are no parentheses with parameters.
  • There are no curly braces for the function body.
  • The syntax is easy to write and read.
  • return keyword is not required for a single expression.

Syntax for Arrow Function

Arrow function for single parameter and a single return statement

code.png

For more than one parameter with a single return statement

code.png

For multiple statements

code.png

Some importants points

  • No parentheses require for a single parameter
  • no return keyword require or a single expression
  • return keyword require for multiple expression
  • parentheses require for multiple parameters.

Thank you