Ik begrijp dat je in JavaScript kunt schrijven:
if (A && B) { do something }
Maar hoe implementeer ik een OR zoals:
if (A OR B) { do something }
Antwoord 1, autoriteit 100%
Gebruik gewoon de logische “OR”-operator , dat is ||
.
if (A || B)
Antwoord 2, autoriteit 33%
Het is vermeldenswaard dat ||
ook true
zal retourneren als BEIDE A
en B
true
.
Als u in JavaScript zoekt naar A
of B
, maar niet beide, moet u iets doen dat lijkt op :
if( (A && !B) || (B && !A) ) { ... }
Antwoord 3, autoriteit 6%
Antwoord 4, autoriteit 5%
if (A || B) { do something }
Antwoord 5, autoriteit 4%
||
is de operator of.
if(A || B){ do something }
Antwoord 6, autoriteit 3%
hier is mijn voorbeeld:
if(userAnswer==="Yes"||"yes"||"YeS"){
console.log("Too Bad!");
}
Dit zegt dat als het antwoord Ja, ja of Ja is, hetzelfde zal gebeuren
Antwoord 7
Je kunt ook reguliere uitdrukkingengebruiken :
var thingToTest = "B";
if (/A|B/.test(thingToTest)) alert("Do something!")
Antwoord 8
Als we reguliere expressies gaan noemen, kunnen we net zo goed de switch
-statement.
var expr = 'Papayas';
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas': // Mangoes or papayas
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log('Sorry, we are out of ' + expr + '.');
}
Antwoord 9
Er is meer dan één voorwaarde-instructie nodig om de operator OR(||)
te gebruiken in if-voorwaarden en notatie ||
.
if(condition || condition){
some stuff
}
Antwoord 10
Je kunt Like gebruiken
if(condition1 || condition2 || condition3 || ..........)
{
enter code here
}
Antwoord 11
Gebruik gewoon ||
if (A || B) { your action here }
Opmerking: met string en nummer. Het is ingewikkelder.
Controle dit voor diepe ondering: