Arrow function notation in Javascript : why?

Hello everybody!

This might be a stupid question, but why (or in what case) would you want to use a shorthand notation of a function in Javascript? I get that it’s less writing, but still…
Am I missing something?

And, less reading.

const f = x => x ** 2

Take out the, const f = and we still have a function expression. In many situations this conciseness is beneficial on all counts.

Consider the alternative…

const f = function (x) {
 return x ** 2
}

The typing is less a concern than what is moot. Some situations favor the shortened syntax. That is, some. This is not a replacement, but an addition to the syntax when it best serves our needs. If braces are involved, switch to function.


Disclaimer

We can use braces in an arrow function, which requires a return, but it doesn’t give our function all of the properties of a standard function. It means narrowing down our use cases, which is appropriate, and should be part of the line of thinking. There is no advantage to using arrow function syntax that we don’t design into our program. We’re merely leveraging a promise.

1 Like

Cheers for the explenation!

1 Like