Skip to content

Instantly share code, notes, and snippets.

@oliverjumpertz
Created December 24, 2020 20:48
Show Gist options
  • Save oliverjumpertz/a9ef9386f6c99583ac66d4f5cd93c581 to your computer and use it in GitHub Desktop.
Save oliverjumpertz/a9ef9386f6c99583ac66d4f5cd93c581 to your computer and use it in GitHub Desktop.
Switching over anything
function getCheering(followers) {
// If you were to switch over followers, you'd be surprised,
// because you'd always hit the default case.
// By switching over true, all cases get evaluated properly, although they
// contain numeric ranges!
switch (true) {
case followers >= 2000:
return "Wow, I'm speechless. I'm so happy, thank you!!!";
case followers >= 1000:
return "Wow, this is so great!";
case followers >= 500:
return "Awesome!";
case followers >= 100:
return "Wooooo!";
default:
return "Yay!";
}
}
const cheer = getCheering(2125);
console.log(cheer); // => "Wow, I'm speechless. I'm so happy, thank you!!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment