Skip to content

Instantly share code, notes, and snippets.

@nhuxhr
Created July 21, 2021 22:50
Show Gist options
  • Save nhuxhr/f2eaa85b27e642275f19ff37345066ca to your computer and use it in GitHub Desktop.
Save nhuxhr/f2eaa85b27e642275f19ff37345066ca to your computer and use it in GitHub Desktop.
Handle empty args that's passed to a function in JavaScript
/**
* Handle empty args that's passed to a function in JavaScript
* @author JSX Clan <jsxclan.dev@gmail.com>
*/
const isRequired = (name) => {
throw Error(`${name} is missing.`);
}
const setUsername = (username = isRequired('Username')) => {
console.log(`Username: ${username}`);
// Do something with 'username'
// If 'username' is not provided,
// these lines of code will never be reached.
}
setUsername() // Username is missing.
setUsername('jsxclan') // Username: jsxclan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment