1

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> 
4

1 Answer 1

3

You can use Array#includes or Array#indexOf and check wether the input is included in your array where you whitelist allowed strings (or other types - depending on your input type)

Demo with includes

let arr = ['random text', '3', 'more random test', 'lorem ipsum']; function submit() { input = document.getElementById("userInput").value; if(arr.includes(input)) { alert('yaaay that was easy'); } }
<input id="userInput"> <button onclick="submit()">Submit</button>

Sign up to request clarification or add additional context in comments.

1 Comment

ty it's help me understand

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.