Skip to content

Instantly share code, notes, and snippets.

@emilong
Last active May 4, 2016 21:50
Show Gist options
  • Save emilong/0f8bccbf3a683b694211bd85ea7f9a5f to your computer and use it in GitHub Desktop.
Save emilong/0f8bccbf3a683b694211bd85ea7f9a5f to your computer and use it in GitHub Desktop.
/**
* 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';
}
})();
# 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