DEV Community

stalin s
stalin s

Posted on

Arrow function variation in javascript.

I have already discussed the arrow function in JavaScript in one of blogs.Arrow function .

In this blog let's deep dive in arrow function variations.

There are two factors that affects arrow functions.

1) The number of arguments required.
2) Whether you'd like an implicit return.

Number of arguments

zero arguments. : we can substitute the parenthesis with an underscore (_).

const zeroArgs = () => {/* do something */}
const zeroWithUnderscore = _ => {/* do something */}
Enter fullscreen mode Exit fullscreen mode

One arguments. : we can remove parenthesis.

const oneArg = arg1 => {/* do something */}
const oneArgWithParenthesis = (arg1) => {/* do something */}
Enter fullscreen mode Exit fullscreen mode

Two or More. : we will be the normal arrow function syntax.

 const twoOrMoreArgs = (arg1, arg2) => {/* do something */}
Enter fullscreen mode Exit fullscreen mode

Return type.

The below example would clearly describes how arrow functions handles return type.

// 3 lines of code with a normal function
const sumNormal = function (num1, num2) {
  return num1 + num2
}
Enter fullscreen mode Exit fullscreen mode
// Can be replaced with one line of code with an arrow function
const sumArrow = (num1, num2) => num1 + num2
Enter fullscreen mode Exit fullscreen mode

Happy learning.

Top comments (0)