Created
January 5, 2022 10:57
-
-
Save elfacht/27c954ad791030ff2ad43b137f1851ea to your computer and use it in GitHub Desktop.
if to object
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
// ❌ Before | |
function getErrorMessage(error) { | |
if(error === 'username') { | |
return `Username is required` | |
} else if (error === 'email') { | |
return `Don't forget your email!` | |
} else if (error === 'password') { | |
return `Please enter your password` | |
} | |
} | |
// ✅ After | |
function getErrorMessage(error) { | |
const errorResponses = { | |
username: `Username is required`, | |
email: `Don't forget your email!`, | |
error: `Please enter your password` | |
} | |
return errorResponses[error] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment