lundi 9 avril 2018

If (Condition == Any Array Value) In JavaScript [duplicate]

This question already has an answer here:

So I'm trying to make a if statement with many possibilities, but what ended up happening is now I have WAY too many || in my if.

I thought maybe it would be possible to throw all of the possible values into a array, and then only do condition == array or something.

Anyway here's my code. Please help. Thanks in advance.

<body>
<input id="userInput">
<button onclick="Submit()">Submit</button>
<script>
function Submit() {
var input = document.getElementById("userInput").value;

if (input == "random text" || input == 3 || input == "more random text" 
|| input == false || input == "another random string.")
{
  // Do Something
}
</script>
</body>

Aucun commentaire:

Enregistrer un commentaire