English 中文(简体)
How to use OR condition in a JavaScript IF statement?
原标题:

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 }
最佳回答

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

问题回答

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) ) { ... }
if (A || B) { do something }

|| is the or operator.

if(A || B){ do something }

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

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");

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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签