0

How can I use a "or" contidition in array

Like I want to write var a = ["hi" ,"hello" ,"hey"] so how can I set it user enters hi or hello or hey then the output will be hi in the array

10th Nov 2020, 4:14 PM
Charchit Dahiya
Charchit Dahiya - avatar
7 Answers
+ 3
Charchit Dahiya you can use if (array.includes(value)) { // its inside array } else { // not inside } It is the same as: if (value === "hi" || value === "hello" || value === "hey") { }
10th Nov 2020, 5:25 PM
maf
maf - avatar
+ 3
Charchit Dahiya includes means (is it inside the array or not?) let arr = [ 1,2,3 ] arr.includes(1) // true arr.includes(4) // false
10th Nov 2020, 5:28 PM
maf
maf - avatar
+ 2
Do you really have to use OR operator? you can use `indexOf` method to check whether a word was in the array though. (Edit) I thought you said "have to use OR operator" var a = [ "hi", "hello", "greetings" ]; if(-1 != a.indexOf(prompt("Enter a word"))) { alert("Found"); } else { alert("Not found"); }
10th Nov 2020, 4:21 PM
Ipang
+ 2
If you must use OR operator then it means you'd be using `if` statement where there are N (array items count) conditions evaluated, each chained by OR operator. Maybe something like if(word === "hi" || word === "hello" || word === "hey") // code here Assuming <word> is user input ...
10th Nov 2020, 4:30 PM
Ipang
+ 1
Please can you elaborate. It will be very sweet of you. Thanks in advance
10th Nov 2020, 5:26 PM
Charchit Dahiya
Charchit Dahiya - avatar
+ 1
Thanks
10th Nov 2020, 5:29 PM
Charchit Dahiya
Charchit Dahiya - avatar
0
No I have to use OR operator any function or method for using it in arrays
10th Nov 2020, 4:25 PM
Charchit Dahiya
Charchit Dahiya - avatar