Skip to content

Instantly share code, notes, and snippets.

@michaelenglo
Last active May 18, 2020 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelenglo/74577990a139ca3c7ff5c2d501df43d4 to your computer and use it in GitHub Desktop.
Save michaelenglo/74577990a139ca3c7ff5c2d501df43d4 to your computer and use it in GitHub Desktop.
The world's cleanest switch statement.
This is how regular switches are typically made:
function regularSwitch(case) {
switch(case) {
case "default":
return "green";
case "ignored":
return "red";
case "selected";
return "grey";
default:
return undefined;
}
}
This is a slightly cleaner way to write simple mapping switch:
function simpleSwitch(case) {
return {
"default": "green",
"ignored": "red",
"selected": "grey",
}[case];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment