Shorthand for multiple ||'s in a If statement?

Hello, I was working on the Rock, Paper, or Scissors exorcise and have a question.
[https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript](https://Rock, Paper, or Scissors)

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
    return userInput;
  } else {
    console.log('Error, please input rock, paper or scissors only');
  }
};

for the if userinput equals rock or if userinput equals paper or if userinput equal scissors part is there a way to shorthand it to if userinput equals rock, paper, or scissors? like userInput === 'rock' || 'paper' || 'scissors kind of deal?

You could use an array to do that too.

if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){

could be done as

const possibilities = ['rock', 'paper', 'scissors', 'bomb'];
if (possiblilites.includes(userInput)) { 
4 Likes

As a learning exercise, I invite you to consider why your suggestion of userInput === 'rock' || 'paper' || 'scissors would not work as you want it to. :slight_smile:

1 Like