title: Pattern matching in JavaScript?
body: <p>I need to create a variable in JavaScript and assign it's value based on a condition. This works but feels a bit verbose: </p>
  
  <pre><code>  const color = (() =&gt; {
      switch (type) {
        case "primary":
          return CONSTANTS.colors.primary;
        case "secondary":
          return CONSTANTS.colors.secondary;
        case "tertiary":
          return CONSTANTS.colors.tertiary;
        case "positive":
          return CONSTANTS.colors.positive;
        case "negative":
          return CONSTANTS.colors.negative;
        case "disabled":
          return CONSTANTS.colors.disabled;
        default:
          throw new Error("A backgroundColor condition was missed");
      }
    })();
  </code></pre>
  
  <p>Is what I'm trying to do called "pattern matching"? Ive read that JavaScript doenst have this feature but Im not totally sure what it is.</p>
  
  <p>Is there a more concise way of writing the code above? I could have lots of <code>if</code> statement but this feels messier and requires the variable to be <code>let</code> not <code>const</code>. </p>
  
  <pre><code>let color:
  if (type === "primary") {
      color = CONSTANTS.colors.primary;
  } else if(type === "secondary") {
      color = CONSTANTS.colors.secondary;
  } else if(type === "tertiary") {
      color = CONSTANTS.colors.tertiary;
  } else if(type === "secondary") {
      color = CONSTANTS.colors.secondary;
  } else if(type === "positive") {
      color = CONSTANTS.colors.positive;
  } else if(type === "negative") {
      color = CONSTANTS.colors.negative;
  } else if(type === "disabled") {
      color = CONSTANTS.colors.disabled;
  }
  </code></pre>