Created
September 22, 2019 16:48
-
-
Save NyaGarcia/910f634e9b7b63af592d8ebcd408fdd5 to your computer and use it in GitHub Desktop.
Short circuiting with && operator
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 starterPokemon = ['Squirtle', 'Charmander', 'Bulbasur']; | |
function choosePokemon(pokemon) { | |
return pokemon[Math.floor(Math.random() * pokemon.length)]; | |
} | |
//Checks if the starterPokemon array has any elements and runs the choosePokemon function | |
console.log(starterPokemon.length > 0 && choosePokemon(starterPokemon)); | |
//It's equivalent to doing this: | |
if (starterPokemon.length > 0) { | |
choosePokemon(starterPokemon); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment