Making parameters required

Functions in JavaScript don’t have “required” parameters. We can emulate them by setting a function that throws an error as a parameter’s default value. This assigned function call only happens if we omit the parameter, making it required.

// calling this function always throws an error
const throwIfMissing = parameter => {
  throw new Error(`Parameter '${parameter}' needs to be defined.`)
}

// if we don’t pass a `thing`, that function will be called as the default
const askAboutFavorite = (thing = throwIfMissing('thing')) => {
  console.log(`What is your favorite ${thing}?`)
}

// not passing anything, or passing `undefined`, causes the error to happen
askAboutFavorite()           // Error: "Parameter 'thing' needs to be defined."
askAboutFavorite(undefined)  // Error: "Parameter 'thing' needs to be defined."

// passing any defined value is fine
askAboutFavorite(null)       // "What is your favorite null?"
askAboutFavorite('color')    // "What is your favorite color?"

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.