Implicit object return in arrow functions

If your arrow function immediately returns an object and does nothing else, you can wrap that object in parentheses instead of writing return.

// we can be explicit and write `return` before the object we want to return
const myFunction = value => {
  return {
    key: value,
    length: value.length
  }
}

// wrapping the object in parentheses also returns it
const myFunction = value => ({
  key: value,
  length: value.length
})

// if anything else happens in the function, like assigning variables or
// calling other functions, we HAVE to be explicit and write `return`
const myFunction = value => {
  const length = value.length

  return {
    key: value,
    length: length
  }
}

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.