How does Javascript interpret parantheses?

I am currently working through functional programming exercises and working with the map method for the first time.

The relevant exercise requires map to be called on an array of objects, and return an array of objects with only two of the parent object’s properties.

When I write the callback function, the solution works out to:

var ratings = => ({ title: item.Title, rating: item.imdbRating }))

This solution requires a parantheses around the returned object for the callback function inside map.
My question is, why? Why isn’t the following acceptable:
var ratings = => { title: item.Title, rating: item.imdbRating })

The full code for the exercise is here:

1 Like

When JavaScript encounters a { immediately after a =>, it assumes you are creating a code block, so you must wrap the { and } in parentheses, so JavaScript understands it is a literal object you are wanting to return and not the start of a code block.

The following would be perfectly valid though:

const newArr = => {
  return { prop1: elem.Title, prop2: elem.imdbRating };
1 Like