ECMAScript 6 arrow function that returns an object

When returning an object from an arrow function, it seems that it is necessary to use an extra set of {} and a return keyword because of an ambiguity in the grammar.

That means I can’t write p => {foo: "bar"}, but have to write p => { return {foo: "bar"}; }.

If the arrow function returns anything other than an object, the {} and return are unnecessary, e.g.: p => "foo".

p => {foo: "bar"} returns undefined.

A modified p => {"foo": "bar"} throws SyntaxError: unexpected token: ‘:‘”.

Is there something obvious I am missing?

48 thoughts on “ECMAScript 6 arrow function that returns an object”

  1. 369727 232122Hi there, just became aware of your weblog by means of Google, and found that its genuinely informative. Im gonna watch out for brussels. I will appreciate in the event you continue this in future. Lots of folks will probably be benefited from your writing. Cheers! 283976


Leave a Comment