Mohsin Mohsin - 17 days ago 6
Javascript Question

How to use || and && together in one if statement

I am trying to check if the

firstday
is equal to five or 6, in both cases if
totalDays
is equal to 31 then do something, for that purpose which statement is correct?

Code 1:

if (firstday > 5 || firstday > 6 && totalDays == 31){}


Code 2:

if (firstday > 5 && totalDays == 31 || firstday > 6 && totalDays == 31) { }

Answer

This is the correct answer:

if ((firstday == 5 || firstday == 6) && totalDays == 31){
    //Do something
}
Comments