Created
July 21, 2021 22:50
-
-
Save nhuxhr/f2eaa85b27e642275f19ff37345066ca to your computer and use it in GitHub Desktop.
Handle empty args that's passed to a function in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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