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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay