if ... else

if is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.

If the expression is true (i.e. today < endDate) then the block following if is executed.

          var endDate = NSOA.form.getValue('end_date');
var today = new Date();
if (today < endDate) {
    // statements to execute if we haven't started this yet  
} 

        

If the expression is false (i.e. today < endDate in not true) the optional block following else is executed.

          var endDate = NSOA.form.getValue('end_date');
var today = new Date();
if (today < endDate) {
    // statements to execute if we haven't started this yet  
} else {
    // statements to execute if we have started
} 

        
Note:

‘else’ is optional, but ‘if’ must be present

You can chain together as many if ... else statements as required.

          var type = NSOA.form.getValue('type');
if (type == 0) {
    // statements to handle type 0  
} else if (type == 1) {
    // statements to handle type 1
} else if (;type == 2) {
    // statements to handle type 2
} else{
    // statements to handle all other types
} 

        
Tip:

Rather than creating a long if .. else chain, use the switch statement.

Note:

This is just a series of if statements, where each if is part of the else clause of the previous statement. Each condition is evaluated in sequence. The first block with its condition to evaluate true is executed and then the whole chain is exited. If no condition is true then the final else block is executed.

See also: