Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify multiple conditions in an if statement in javascript

Tags:

javascript

Here is how I mention two conditions if this or this

if (Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')     PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value; } 
like image 510
adilahmed Avatar asked Jan 03 '12 09:01

adilahmed


People also ask

Can you have 3 conditions in an if statement JavaScript?

Using either “&&” or “||” i.e. logical AND or logical OR operator or combination of can achieve 3 conditions in if statement JavaScript.

How do you write multiple if statements in JavaScript?

We can also write multiple conditions inside a single if statement with the help of the logical operators && and | | . The && operators will evaluate if one condition AND another is true. Both must be true before the code in the code block will execute.

How do you add multiple conditions in an if statement?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

Can IF statement have 2 conditions?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.


2 Answers

just add them within the main bracket of the if statement like

if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) {             PageCount= document.getElementById('<%=hfPageCount.ClientID %>').value; } 

Logically this can be rewritten in a better way too! This has exactly the same meaning

if (Type == 2 && (PageCount == 0 || PageCount == '')) { 
like image 171
AlanFoster Avatar answered Sep 24 '22 02:09

AlanFoster


Here is an alternative way to do that.

const conditionsArray = [     condition1,      condition2,     condition3, ]  if (conditionsArray.indexOf(false) === -1) {     "do somthing" } 

Or ES7+

if (!conditionsArray.includes(false)) {    "do somthing" } 
like image 25
Glen Thompson Avatar answered Sep 24 '22 02:09

Glen Thompson