150

I understand that in JavaScript you can write:

if (A && B) { do something }

But how do I implement an OR such as:

if (A OR B) { do something }
Ernest Friedman-Hill
  • 79,064
  • 10
  • 147
  • 183
sadmicrowave
  • 37,748
  • 34
  • 105
  • 178

11 Answers11

292

Simply use the logical "OR" operator, that is ||.

if (A || B)
Luca Rocchi
  • 6,013
  • 1
  • 22
  • 23
95

Worth noting that || will also return true if BOTH A and B are true.

In JavaScript, if you're looking for A or B, but not both, you'll need to do something similar to:

if( (A && !B) || (B && !A) ) { ... }
double-beep
  • 4,567
  • 13
  • 30
  • 40
user113716
  • 310,407
  • 61
  • 442
  • 435
  • Shouldn't be first phrase be "Worth noting that || will return true if EITHER var A OR var B is true" ?? It implies what you mentioned is (true | true) = true. which is common and understood. – Punith Raj Jan 23 '15 at 07:44
  • 14
    (A && !B) || (B && !A) **can be replaced with** A ^ B which is much smoother –  Oct 28 '15 at 14:31
  • 1
    @Murplyx: In most cases yes, but numbers outside the 32 bit range can fail. `(Math.pow(2,32)-1) ^ 0; // -1 (success)` ... `Math.pow(2,32) ^ 0; // 0 (failure)` –  May 12 '16 at 00:44
  • `if (A ? !B : B) {...` would be a shorter substitute that wouldn't have the 32-bit limitation. Or maybe `if (!A != !B) {...` –  May 12 '16 at 00:52
  • 1
    @squint Why would a true or false ever be outside of the 32 bit range hence they are only 0 or 1, and btw if you compare numbers just use !!n to get the boolean value. –  May 26 '16 at 17:17
  • @Murplyx: Who said the values were only `true` and `false`? We can't assume that in JS. WRT `!!n`, that was the point of my last comment... `!A != !B` is the same as `!!A != !!B`, which provide the XOR behavior. –  May 26 '16 at 19:53
16

Use the || operator.

Skilldrick
  • 67,147
  • 33
  • 171
  • 227
15
if (A || B) { do something }
Dolbz
  • 2,038
  • 1
  • 16
  • 25
12

|| is the or operator.

if(A || B){ do something }
rosscj2533
  • 8,965
  • 7
  • 40
  • 55
10

here is my example:

if(userAnswer==="Yes"||"yes"||"YeS"){
 console.log("Too Bad!");   
}

This says that if the answer is Yes yes or YeS than the same thing will happen

ElGavilan
  • 6,214
  • 16
  • 25
  • 35
Dyljam1234
  • 181
  • 1
  • 2
  • 1
    Does your answer improve upon any existing answer? It's a specific use case? – emecas Dec 30 '14 at 21:03
  • Is it work? I code like that but it's syntax error. I code like this. `if (name === 'Jam' || name === 'Jem' || name == 'Jum')` – Penguin Jun 04 '15 at 06:17
  • 10
    Yes, I discovered the hard way that you have to include each statement separately. I worked out that `if (number === 1||2||3)` is like `while (true)`; the second and third conditions ask if 2 is 2 and/or 3 is 3. They always resolve as true to the statement always passes. There goes my plan to reduce the character count. Keeping the statements in parenthesis does make it easier to read though. – TimSmith-Aardwolf Jul 13 '15 at 15:03
  • 5
    Just much better to use .toLowerCase() instead of having to check all different case variants. – AquaAlex Sep 18 '15 at 15:51
  • 1
    var choice = prompt("Do you choose rock, paper or scissors?").toLowerCase(); if (userChoice != ("paper"||"rock"||"scissors")) { console.log("Invalid Choice made"); } – AquaAlex Sep 18 '15 at 15:53
2

One can use regular expressions, too:

var thingToTest = "B";
if (/A|B/.test(thingToTest)) alert("Do something!")

Here's an example of regular expressions in general:

var myString = "This is my search subject"
if (/my/.test(myString)) alert("Do something here!")

This will look for "my" within the variable "myString". You can substitute a string directly in place of the "myString" variable.

As an added bonus you can add the case insensitive "i" and the global "g" to the search as well.

var myString = "This is my search subject"
if (/my/ig.test(myString)) alert("Do something here");
jgshawkey
  • 1,734
  • 1
  • 8
  • 7
1

If we're going to mention regular expressions, we might as well mention the 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 + '.');
}
0

More then one condition statement is needed to use OR(||) operator in if conditions and notation is ||.

if(condition || condition){ 
   some stuff
}
KARTHIKEYAN.A
  • 14,686
  • 4
  • 102
  • 112
0

You can use Like

if(condition1 || condition2 || condition3 || ..........)
{       
     enter code here
}
Abhilash Reddy
  • 1,477
  • 1
  • 10
  • 11
-1

Just use ||

if (A || B) { your action here }

Note: with string and number. It's more complicated.

Check this for deep understading:

haotang
  • 5,302
  • 32
  • 43