Why does map Method+arrow function needs to return obj in parentheses?

{} is used to wrap a block of code. {} is also the syntax for an object literal.

someLabel: someThing is valid (if obscure) JS. someLabel: someThing is also how you define keys: values in an object literal.

, is an operator in JS. It is also used to seperate key:value pairs in an object literal.

Bearing that in mind, how does the JS interpreter know what this is, given it can be two completely different things:

const example = () => { foo: 1, bar: 2 };

That gets interpreted as

const example = () => {
  return foo: 1, bar: 2;
};

() would say “evaluate what’s inside the parentheses before doing anything else”, so

const example = () => ({ foo: 1, bar: 2 });

Gets interpreted as

const example = () => {
  return { foo: 1, bar: 2 };
};
1 Like