Last active
September 23, 2020 12:16
-
-
Save NyaGarcia/7acbeb0c738932c38114a9bab9261af6 to your computer and use it in GitHub Desktop.
Using an object literal to replace a switch statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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