Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active September 23, 2020 12:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NyaGarcia/7acbeb0c738932c38114a9bab9261af6 to your computer and use it in GitHub Desktop.
Save NyaGarcia/7acbeb0c738932c38114a9bab9261af6 to your computer and use it in GitHub Desktop.
Using an object literal to replace a switch statement
const pokemon = {
Water: 'Squirtle',
Fire: 'Charmander',
Plant: 'Bulbasur',
Electric: 'Pikachu'
};
function getPokemon(type) {
return pokemon[type] || 'Mew';
}
console.log(getPokemon('Fire')); // Result: Charmander
// If the type isn't found in the pokemon object, the function will return the default value 'Mew'
console.log(getPokemon('unknown')); // Result: Mew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment