Assigning a variable using a switch statement in Javascript

Here's a simple trick to assign a variable using a switch statement in Javascript. It uses IIFE pattern (http://benalman.com/news/2010/11/immediately-invoked-function-expression/).

Put simply, it looks like this:

var price = (function(color) {
  switch(color) {
    case 'red':
      return 10;
    case 'blue':
      return 20;
    default:
      return 30;
  }
})('blue');

console.log(price); // Will print 20