167

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 }
TylerH
  • 20,799
  • 66
  • 75
  • 101
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180

8 Answers8

317

Use the logical "OR" operator, that is ||.

if (A || B)

Note that if you use string comparisons in the conditions, you need to perform a comparison for each condition:

if ( var1 == "A" || var1 == "B" )

If you only do it in the first one, then it will always return true:

if ( var1 == "A" || "B" ) //don't do this; it is equivalent to if ("B"), which is always true

The official ECMAScript documentation can be found here

TylerH
  • 20,799
  • 66
  • 75
  • 101
Luca Rocchi
  • 6,272
  • 1
  • 23
  • 23
102

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
  • 5,031
  • 17
  • 33
  • 41
user113716
  • 318,772
  • 63
  • 451
  • 440
  • 1
    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
  • 17
    (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
19
if (A || B) { do something }
Dolbz
  • 2,078
  • 1
  • 16
  • 25
17

Use the || operator.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
13

|| is the or operator.

if(A || B){ do something }
rosscj2533
  • 9,195
  • 7
  • 39
  • 56
6

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,610
  • 16
  • 27
  • 36
Dyljam1234
  • 189
  • 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
  • 11
    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
  • doesn't make any sense to write `"Yes"||"yes"||"YeS"`... I can't understand what is your intention. – Raskul Oct 19 '22 at 12:51
  • 1
    This is not correct. – TylerH Jan 11 '23 at 20:07
4

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
  • 2,034
  • 1
  • 9
  • 8
1

You may also want to filter an IF statement when condition1 equals 'something' AND condition2 equals 'another thing' OR 'something else'. You can do this by placing condition2 in another set of brackets eg...

if (condition1 === 'x' && (condition2 === 'y' || condition2 === 'z')) {
    console.log('do whatever')
}

If condition2 is NOT in it's own brackets then condition1 has to be x AND condition2 has to be y for the function to trigger...

... but it will also trigger if condition2 is z, regardless of what condition1 is. Which may be okay depending on your use case, but something to be mindful of.

Mr. J
  • 1,524
  • 2
  • 19
  • 42