twitch
Streaming on Twitch twice a week

Implicit object return in arrow functions

JavaScript

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
  }
}

More fire tips

Relative timestamps with Intl

With JavaScript’s built-in formatter for relative timestamps, we can build strings like “2 months from now” without having to use third party libraries.

JavaScript
Read fire tip

Read all fire tips →