Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESCMAScript 6 arrow functions - parentheses around parameter

I'm new to javascript and cannot understand simple thing - what is the difference between

(x) => { return x*2}

and

x => { return x*2} //(just for example, may not work)

Can someone explain or give link for description?

like image 800
Dmitry Avatar asked Sep 08 '26 11:09

Dmitry


1 Answers

The parenthesis around input arguments (x in this case) are only required when there are two or more input arguments. With just one (as you've shown here), the two statements are identical.

(x) => { return x * 2; } is the same as x => { return x * 2; }

But,

(x, y) => { return x * y; }

Requires parenthesis around the input arguments.

See this for all the gory details!

like image 85
Scott Marcus Avatar answered Sep 10 '26 23:09

Scott Marcus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!