Function declaration () => ({}); vs () => {{}};

Hi All,

I’ve observed there are 2 ways to declare inline functions as below

Way 1

const getMousePosition = (x, y) => ({ x, y });

Way 2

const createPerson = (name, age, gender) => {
  "use strict";
  return { name, age, gender };
};

Please clarify when to use parenthesis vs curly braces

Thanks,
Vikram

The first is returning an object literal, which requires the parentheses so it isn’t confused with the second form. The second form is using the curly braces not for an object, but to contain multiple statements, the same as you’d do if you used function () syntax.

So if you’re returning an object in just one expression, you need the first, and if you have multiple statements in your function, you need the second.

1 Like

Thanks for quick clarification in detail.

Regards,
Vikram