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
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") {
}
+ 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
+ 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");
}
+ 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 ...
+ 1
Please can you elaborate. It will be very sweet of you. Thanks in advance
+ 1
Thanks
0
No I have to use OR operator any function or method for using it in arrays