Understanding .map() ES6 vs ES5 Syntax

Hi Everyone.

So I’m currently going through the Javascript course, and there is a particular section in the “Using .map() to extract data from an array” that I don’t understand.

In ES6, I wrote the code as:

var rating = watchList.map(e => ({
  title: e.Title,
  rating: e.imdbRating
}));

Which works perfectly fine, however if I were to change it to ES5.

var rating = watchList.map(function(e){
  title: e.Title,
  rating: e.imdbRating
});

It spits out a syntax error. Can someone explain the reason for this?

var rating = watchList.map(function(e){
  title: e.Title,
  rating: e.imdbRating
});

This function doesn’t return anything and is also missing the braces ({}) around the object.

Got it!

var rating = watchList.map(function(e){
  return ({title: e.Title,
  rating: e.imdbRating})
});

Thanks for the help!

1 Like

Good job figuring it out. Happy coding!

you are suppose to use the return key word . example.

return {
  title: e.Title,
  rating: e.imdbRating
}
});

not necessarily, with arrow syntax if your function body is just a return statement you can use implicit return

the following functions do the same thing

x => {
   return x/2
}

// the above is the same as the below 
x => x/2