Returning objects with implicit return

Hey ,

I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.

Please upgrade (use this link) access this content.

I'm super eager to help you learn more!

Returning objects with implicit return

You make arrow functions perform an implicit return if you follow two rules:

  1. You write only one line of code in the function
  2. That line of code is not enclosed in curly braces ({})

The second rule can be relaxed a little if you want to return an object.

Here’s how you would return an object if you did not use implicit returns.

const functionName = _ => {
  return {
    prop: 'value'
  }
}

To return objects with an implicit return, you can wrap your object within parenthesis.

const functionName = _ => ({prop: 'value'})

Exercise

Create an arrow function that uses implicit returns to return an object.

Answers

const arrow = _ => ({key: 'value'})