Use of paranthesis in arrow functions in this solution


const ratings = watchList.map(item => ({
title : item["Title"],
rating : item["imdbRating"]}));

Why the need for the paranthesis covering {} after the item => part. When i delete them, missing semicolon error occurs. but it has worked for other challenges. i used to implement it like

()=>{return blabla}

but is it a special case not working for map ?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Use the map Method to Extract Data from an Array

Link to the challenge:

Both function and object bodies are encapsulated by curly braces. In the particular case, you use parenthesis to distinguish that the braces represent an object body and not a function body, while function braces are omitted. If you were to remove the parenthesis, the compiler will read the braces as function body and within that function body it will find the following syntax:

title : item["Title"],
rating : item["imdbRating"]

Put that code bare and you will notice its not a valid JS syntax

And recall how arrow functions work. If we omit the function braces, whatever follows is to be returned by default.
Your example is similar to this code:

const ratings = watchList.map(item =>{
  //that is function body
  return {
    //that is object body
    title : item["Title"],
    rating : item["imdbRating"]
  }
});
1 Like

I understand it now. Thanks for great explanation. :innocent:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.