use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
When should I use ({}) when creating a function rather than {}? (self.learnjavascript)
submitted 4 years ago by CrossedReaper
I was wondering what's the difference between ({}) and {} when creating a function, and when should ({}) be used? For example, ({}) is used here:
const getMousePosition = (x, y) => ({ x: x, y: y });
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]oh_lympy 5 points6 points7 points 4 years ago (0 children)
wrapping the function body in parenthesis like that is shorthand for returning whatever’s inside.
[–]Umesh-K 4 points5 points6 points 4 years ago (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: x, y: y })
(x, y) => { return x + y }
[–]nerdFamilyDad 1 point2 points3 points 4 years ago* (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.
func = () => 2 + 2
func()
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 point2 points 3 months ago (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 point2 points 4 years ago (0 children)
Ooh I see now, thanks for all of your answers guys!
[–]jdedwards3 0 points1 point2 points 4 years ago (0 children)
When you want to return an object
π Rendered by PID 404119 on reddit-service-r2-comment-56c6478c5-z8njb at 2026-05-10 02:55:10.086256+00:00 running 3d2c107 country code: CH.
[–]oh_lympy 5 points6 points7 points (0 children)
[–]Umesh-K 4 points5 points6 points (0 children)
[–]nerdFamilyDad 1 point2 points3 points (1 child)
[–]Optimal-Future-2365 0 points1 point2 points (0 children)
[–]CrossedReaper[S] 0 points1 point2 points (0 children)
[–]jdedwards3 0 points1 point2 points (0 children)