Why we use parantheses around returning object

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

Hello and welcome to the freeCodeCamp community~!

Arrow function syntax has a fun feature where a single-line function has an implicit return. So:

const example = () => 'Hello World!'

This function will return Hello World!.

For multi line functions, the function code needs to be surrounded by curly braces: { }. But what if we want to return an object?

const returnObject = () => {home: "freeCodeCamp"}

This will actually throw a syntax error! Because we are using curly braces, JavaScript thinks that home: "freeCodeCamp" is NOT an object but instead the code to be executed within the function.

By wrapping that object in parentheses, we tell JavaScript that this is an object and we would like it returned.

1 Like

thanx @nhcarrigan

1 Like