Here's yet another one of Javascript's endless oddities: the switch statement uses strict comparison (think === instead of ==), so something like this works fine:

const threeOptions = value => {
  switch (value) {
    case true:  return 'first option';
    case false: return 'second option';
    default:    return 'third option';
  }
};

console.log(threeOptions(true)); // 'first option'
console.log(threeOptions(false)); // 'second option'
console.log(threeOptions('chicken!')); // 'third option', and not 'first option' as you may guess
Final thoughts

There's some extra weirdness on the source article about being able to put the default block somewhere other than the end. Too disgusting to reproduce here, but, if you enjoy pain, the explanation is one click away.

Previous on JavaScript
Mastodon Mastodon