Matthew
Matthew

Reputation: 55

Multiple OR conditions for an IF statement

Am I doing the multiple OR conditions for an IF statement the right way?

var A0minWidth = 841;
var A0minHeight = 1189;

var A0minWidthBleed = 847;
var A0minHeightBleed = 1195;

UploadedDocNameHeightMM = //(get it from the database)
UploadedDocNameWidthMM = //(get it from the database)

if(UploadedDocNameHeightMM < parseFloat(A0minHeight) || UploadedDocNameWidthMM < parseFloat(A0minWidth) || UploadedDocNameWidthMM > parseFloat(A0minWidthBleed) || UploadedDocNameHeightMM > parseFloat(A0minHeightBleed)) 
{
       //do this
       alert ("Yes! one of those.")
}

Help!

Upvotes: 0

Views: 207

Answers (2)

J0e3gan
J0e3gan

Reputation: 8938

It depends on what your code is supposed to do of course, but syntactically this is correct - e.g. no need to wrap each each expression that is an operand to the logical-OR operators in parentheses like this:

if ((UploadedDocNameHeightMM < parseFloat(A0minHeight)) || (UploadedDocNameWidthMM < parseFloat(A0minWidth)) || (UploadedDocNameWidthMM > parseFloat(A0minWidthBleed)) || (UploadedDocNameHeightMM > parseFloat(A0minHeightBleed)))
{
    alert("Yes! one of those.");
}

Also, the || operator will short-circuit evaluate. Basically it will not evaluate expressions to the right of any expression that evaluates to true.

For more information on || and other JavaScript logical operators including examples check out Mozilla's overview or search on JavaScript logical operators.

Upvotes: 3

Sebastien C.
Sebastien C.

Reputation: 4833

The syntax is correct.

However:

  • parseFloat is not necessary here
  • Are you sure the two first tests are correct (==> Don't you need to check if UploadedDocNameHeightMM > A0minHeight instead of < ?)

Upvotes: 0

Related Questions