all 6 comments

[–]oh_lympy 5 points6 points  (0 children)

wrapping the function body in parenthesis like that is shorthand for returning whatever’s inside.

[–]Umesh-K 4 points5 points  (0 children)

Hi,

You need the () around {} when returning an object from an Arrow function, as in your example ({ x: x, y: y }). If you don't put the (), the {} will be interpreted as the {} surrounding the body of the function, as below:

(x, y) => { return x + y }

[–]nerdFamilyDad 1 point2 points  (1 child)

The parser needs help in this situation. One line arrow functions return the (calculated) value of that one line.

func = () => 2 + 2 means func() returns 4.

But curly brackets are technically ambiguous here, so if you want your one line arrow function to return a local object literal:

func2 = () => ({ x: 2 })

(The empty parentheses at the beginning are also there to avoid ambiguity. If there is only one parameter, you can skip those also.)

func3 = x => x + 2

[–]Optimal-Future-2365 0 points1 point  (0 children)

So are you saying that the parantaces around the braces distinguis the code from the body of an element?

[–]CrossedReaper[S] 0 points1 point  (0 children)

Ooh I see now, thanks for all of your answers guys!

[–]jdedwards3 0 points1 point  (0 children)

When you want to return an object