Last active
May 4, 2016 21:50
-
-
Save emilong/0f8bccbf3a683b694211bd85ea7f9a5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Ruby has these nice case expressions, but switches are statements in JS. | |
* Here's a little convention to make switch statements look a little more like expressions. | |
* | |
* This uses ES6, but of course you could do it with ES5 as well. This pattern is especially | |
* nice in ES6, however, especially when you want to use const instead of let or var. | |
*/ | |
const output = (() => { | |
switch(input) { | |
case 1: return 'one'; | |
case 2: return 'two'; | |
case 3: return 'three'; | |
default: return 'default'; | |
} | |
})(); |
This file contains hidden or 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
# For comparison, the Ruby might look like: | |
output = case input | |
when 1 | |
"one" | |
when 2 | |
"two" | |
when 3 | |
"three" | |
else | |
"default" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment