[javascript] jQuery if statement, syntax

What is a simple jQuery statement that states an operation proceeds only if A and B are true? If A isn't true, stop. If A and B are true, then continue. `

This question is related to javascript if-statement

The answer is


if(A && B){ }

If you're using Jquery to manipulate the DOM, then I have found the following a good way to include logic in a Jquery statement:

$(selector).addClass(A && B?'classIfTrue':'');

I little sophisticated way:

$(selector).is(condition)? alert('true') : alert('false');

Ex:

$("#btn-primary").is(":disabled")? alert('button disabled') : alert('button disabled');


You can wrap jQuery calls inside normal JavaScript code. So, for example:

$(document).ready(function() {
    if (someCondition && someOtherCondition) {
        // Make some jQuery call.
    }
});

It depends on what you mean by stop. If it's in a function that can return void then:

if(a && b) {
    // do something
}else{
    // "stop"
    return;
}

To add to what the others are saying, A and B can be function calls as well that return boolean values. If A returns false then B would never be called.

if (A() && B()) {
    // if A() returns false then B() is never called...
}