Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active October 8, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NyaGarcia/fd8f782a1ad85322f9c9a20f771e93c2 to your computer and use it in GitHub Desktop.
Save NyaGarcia/fd8f782a1ad85322f9c9a20f771e93c2 to your computer and use it in GitHub Desktop.
Using a Map to replace a switch statement
const pokemon = new Map([
['Water', 'Squirtle'],
['Fire', 'Charmander'],
['Plant', 'Bulbasur'],
['Electric', 'Pikachu']
]);
function getPokemon(type) {
return pokemon.get(type) || 'Mew';
}
console.log(getPokemon('Fire')); // Result: Charmander
console.log(getPokemon('unknown')); // Result: Mew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment