Last active
May 18, 2020 22:13
-
-
Save michaelenglo/74577990a139ca3c7ff5c2d501df43d4 to your computer and use it in GitHub Desktop.
The world's cleanest switch statement.
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
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