nick nick - 6 months ago 13
HTML Question

jQuery if condition 1 or condition 2 is true than

I am trying to alert "yes" if ether of the conditions in my if statement are true:

var a = 2;
var b = 1;
if (a = 1 or b = 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}


https://jsfiddle.net/90z7urvd/1/
What do I use for the if, if this is possible?

Answer

a = 1 will set the value 1 to variable a. It is not doing a comparison. For comparison, you use === or ==

=== (Identity operator) is the correct way to compare if both the types are same.

if (a === 1 || b === 1 ) {

=== operator won't do the type conversion before the comparison while == does the type conversion before the comparison.

For your or case, You may use || operator

var bootresul = someExpression || anotherExpression

Corrected code

var a = 2;
var b = 1;
if (a === 1 || b === 1 ) {
    alert('yes');
} else {
    alert('no');
}
Comments