Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Created September 22, 2019 16:48
Show Gist options
  • Save NyaGarcia/910f634e9b7b63af592d8ebcd408fdd5 to your computer and use it in GitHub Desktop.
Save NyaGarcia/910f634e9b7b63af592d8ebcd408fdd5 to your computer and use it in GitHub Desktop.
Short circuiting with && operator
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