Catching unknown keys in lookup tables

The lookup table pattern can replace some long if-else branches and switch statements. We can catch unknown keys we don’t have values for with hasOwnProperty.

const getColorOf = thing => {
  const map = {
    clouds: 'white',
    grass: 'green',
    sky: 'blue',
  }
  
  // throw an error if `thing` isn’t a key in the map
  if (!map.hasOwnProperty(thing)) {
    throw new Error(`Color not defined for: ${thing}`)
  }

  // if we get here, `thing` IS a key in the map
  return map[thing]
}

getColorOf('grass')  // ⇒ 'green'
getColorOf('air')    // ⇒ Color not defined for: air
getColorOf()         // ⇒ Color not defined for: undefined

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.