Capt. Horatio T.P. Webb
Scripting IF Statements
VBSCript and Javascript
Parks -- Fall 2001
Last Updated 12PM 9/29/2001

VBSCRIPT
JAVASCRIPT
 
The general form of the VBScript if is:

if <logical condition> then

   .
   .
   statements to execute if the <logical condtion> is true
   (indented to make it easier to read)
   .
   .
else
   .
   .
   .
   statements to execute if the <logical condtion> is false
   (indented to make it easier to read)
   .
   .
   .
end if

The <logical condition> must be either true or false (i.e., Boolean).

 

 
The general form of the Javascript if is:

if ( <logical condition> )

   {
   .
   statements to execute if the <logical condtion> is true
   (indented to make it easier to read)
   .
   }
else
   {
   .
   .
   statements to execute if the <logical condtion> is false
   (indented to make it easier to read)
   .
   .
   .
   }

The <logical condition> must be either true or false (i.e., Boolean).

 

 

These conditional tests can be created in several ways:

 

VBScript Boolean variables

A VBScript variable may be assigned the values true and false using an assignment statement. Thus any variable (such as fred) can be assigned a logical value:

fred=true

or

fred=false

The variable can then be used in the program to change the logical flow of the program based on the true/false value of the variable. The general form above would then be:

if fred then

   .
   .
   statements to execute when fred is true
   .
   .
else
   .
   .
   .
   statements to execute when fred is false
   .
   .
   .
end if

Internally, false is stored as a value of zero. Any non-zero value is assumed to be true.

 

Javascript Boolean variables

A Javascript variable may be assigned the values true and false using an assignment statement. Thus any variable (such as fred) can be assigned a logical value:

fred=true;

or

fred=false;

The variable can then be used in the program to change the logical flow of the program based on the true/false value of the variable. The general form above would then be:

if ( fred )
   {
   .
   .
   statements to execute when fred is true
   .
   }
else
   {
   .
   .
   statements to execute when fred is false
   .
   .
   }

Internally, false is stored as a value of zero. Any non-zero value is assumed to be true.

 

  Javascript if statements can have various layout styles that all work the same way. It is only a matter of style. Consider the following alternatives:

if (logical expression) {
   statement1;
   statement2;
   statement3;
} else {
   statement4;
   statement5;
   statement6; }

or

if (logical expression) {
   statement1;
   statement2;
   statement3; }
else {
   statement4;
   statement5;
   statement6; }

or

if (logical expression)
   {
   statement1;
   statement2;
   statement3;
   }
else
   {
   statement4;
   statement5;
   statement6;
   }
 

VBScript relationals

Two variables can be compared to establish the true/false condition. These logical operators are:

  1. = (equality)
  2. <> (inequality -- i.e., not equal)
  3. < (less than)
  4. > (greater than)
  5. <= (less than or equal to)
  6. >= (greater than or equal to)

These relational operators require two arguments. The general form of the relational is:

if variable 1 =
<>
<
>
<=
>=
variable 1 then

 

Javascript relationals

Two variables can be compared to establish the true/false condition. These logical operators are:

  1. == (equality)
  2. != (inequality -- i.e., not equal)
  3. < (less than)
  4. > (greater than)
  5. <= (less than or equal to)
  6. >= (greater than or equal to)

These relational operators require two arguments. The general form of the relational is:

if (variable 1 ==
!=
<
>
<=
>=
variable 1 )
  Conditional operator

This is also called the "ternary" operator -- so called because it uses three expressions. The general form is:

variable = ( expression1) ? (expression2) : (expression3) ;

The first of the three expressions must always evaluate to "true" or "false", Following the question mark (?) are two expressions separated by a colon (:). If the first expression is "true", the second expression is executed; if the first expression is "false", the third expression is executed. This is equivalent to a simple if statement:

if ( logical expression)
    expression when true;
else
    expression when false;

The conditional operator does NOT have to return a value from expression2) or expression3. These expressions may perform assignments themselves.

Consider the case where expression2 and expression3 are strings:

k = (g > 0) ? "g is positive" : "g is not positive";

Here if g is positive, k is assigned the string value "g is positive". if g is zero or negative, k is assigned the string value "g is not positive". Now consider this:

(g > 0) ? k="g is positive" : k="g is not positive";

This produces exactly the same result as the first example.

The parentheses are optional and may be included only for readability. This conditional (ternary) operator thus allows you to place a simple if statement on a single line.
 

VBScript Type Tests

Several test function can be used on a VbScript variable. They are

  1. IsDate(variable name)

    This function returns true when the variable contains a valid date string. Valid date examples are:

    • 03/25/47
    • 03/25/1947
    • 3/25/1947
    • 03 25 1947
    • 03-25-47
    • Mar 25 1947
    • MARch 25 1947
    • MARCH 25, 1947
    • 25 Mar 1947

    Note that the space, / and - are valid separators.

  2. IsEmpty(variable name)

    Returns true if the variable has never been assigned a value.

  3. IsNull (variable name)

    Returns true if the variable has been specifically assigned the NULL value.

  4. IsNumeric (variable name)

    Returns true if the variable has been assigned a numeric value. Note that a decimal, commas, + and - signs, and the $ sign are numeric.

  5. IsObject(variable name)

    Returns true if the variable is an object (not a string or a number)

  6. IsArray(variable name)

    Returns true if the variable is an array.

 

isNaN

The function is used to test a string to determine if the string contains only numeric values. The IsNaN returns a true or false. For example:

fred=isNan("12345");

returns a true to fred.

fred=isNan("123KJH");

returns a false to fred.

If a variable is assigned the value "NaN" then it will ALWAYS be false in an if statement.

e.g.,

x=parseInt("blue"); // the variable x will be assigned the value "NaN";
alert (x); // this alert will always show the value of x as "NaN"
if (x)
    alert ("x is true");
else
    alert ("x is false -- because NaN always tests as false");

The 2nd alert will always appear because "NaN" is always false.

VBScript NOT (Negation)

The use of NOT reverses the sense of any logical expression. The general form is:

NOT expression

It is most often used with the if statement. Given the variable EOF:

if EOF then      (is true if EOF is true)

but

if NOT EOF then      (is false if EOF is true).

 

Javascript ! (Negation)

The use of ! reverses the sense of any logical expression. The general form is:

! expression

It is most often used with the if statement. Given the variable EOF:

if ( EOF )      (is true if EOF is true)

but

if ( ! EOF )      (is false if EOF is true).

 

VBScript AND (Conjunction)

This allows two logical conditions to be tested simultaneously. The general form is:

if condition 1 AND condition 2 then...

The if statement is evaluated based on the true/false conditions as follows:

condition 1 AND condition 2Outcome
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Note that BOTH conditons must be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.

 

Javascript && (Conjunction)

This allows two logical conditions to be tested simultaneously. The general form is:

if ( condition 1 && condition 2 )...

The if statement is evaluated based on the true/false conditions as follows:

condition 1 && condition 2Outcome
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Note that BOTH conditons must be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.

 

VBScript OR (Disjunction)

This allows two logical conditions to be tested simultaneously. The general form is:

if condition 1 OR condition 2 then...

The if statement is evaluated based on the true/false conditions as follows:

condition 1 OR condition 2Outcome
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Note that EITHER conditon can be true for the combined expressions to be true. Only BOTH conditions being false produces a false result.

 

Javascript || (Disjunction)

This allows two logical conditions to be tested simultaneously. The general form is:

if (condition 1 || condition 2 )...

The if statement is evaluated based on the true/false conditions as follows:

condition 1 || condition 2Outcome
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Note that EITHER conditon can be true for the combined expressions to be true. Only BOTH conditions being false produces a false result.

 

VBScript XOR (Exclusive OR)

This allows two logical conditions to be tested simultaneously. The general form is:

if condition 1 XOR condition 2 then...

The if statement is evaluated based on the true/false conditions as follows:

condition 1 XOR condition 2Outcome
truetruefalse
truefalsetrue
falsetruetrue
falsefalsefalse

Note that exactly ONE condition can be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.

 

  • EQV (Equivalence)

    This allows two logical conditions to be tested simultaneously. The general form is:

    if condition 1 EQV condition 2 then...

    The if statement is evaluated based on the true/false conditions as follows:

    condition 1 EQV condition 2Outcome
    truetruetrue
    truefalsefalse
    falsetruefalse
    falsefalsetrue

  • IMP (Implication)

    This allows two logical conditions to be tested simultaneously. The general form is:

    if condition 1 IMP condition 2 then...

    The if statement is evaluated based on the true/false conditions as follows:

    condition 1 IMP condition 2Outcome
    truetruetrue
    truefalsefalse
    falsetruetrue
    falsefalsetrue

     

  • Javascript XOR (Exclusive OR)

    This allows two logical conditions to be tested simultaneously. The general form is:

    if condition 1 ^ condition 2 then...

    The if statement is evaluated based on the true/false conditions as follows:

    condition 1 ^ condition 2Outcome
    truetruefalse
    truefalsetrue
    falsetruetrue
    falsefalsefalse

    Note that exactly ONE condition can be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.

     

    VBScript One-Sided IF

    The if statement can be used with only the statements to be executed when the true condiditon is encountered.

    the general form is:

    if <condition> then
    .
    .
    . statements to be executed when true
    .
    .
    end if

     

    Javascript One-Sided IF

    The if statement can be used with only the statements to be executed when the true condiditon is encountered.

    the general form is:

    if ( <condition> )
    {
    .
    .
    . statements to be executed when true
    .
    .
    }

     

    VBScript elseif

    To test a sequence of conditions the shorthand form of the if statement uses the elseif form:

    if <condition 1> then
    .
    .
    . statements to be executed when <condition 1> is true
    .
    .
    elseif <condition 2> then

        .
        .
        . statements to be executed when <condition 2> is true
        .
        .
        elseif <condition 3> then

            .
            .
            . statements to be executed when <condition 3> is true
            .
            .
    end if

    Note that there is only one end if that appears at the end of the elseif.

     

    Javascript elseif

    To test a sequence of conditions the shorthand form of the if statement uses the elseif form:

    if ( <condition 1> }
    {
    .
    .
    . statements to be executed when <condition 1> is true
    .
    .
    }
    elseif ( <condition 2> )
        {
        .
        . statements to be executed when <condition 2> is true
        .
        }
        elseif ( <condition 3> )
            {
            .
            . statements to be executed when <condition 3> is true
            .
            .}

     

    VBScript Select Case

    An alternative to using elseif is the Select Case. The general form is:

    Select Case <expression to test>
       case <expression 1>
          code when <expression to test> = <expression 1>
       case <expression 2>
          code when <expression to test> = <expression 2>
       case <expression 3>
          code when <expression to test> = <expression 3>
       case <expression 4>
          code when <expression to test> = <expression 4>
       case else
          code when <expression to test> does not
          equal to any of the other <expressions>
    End Select

    The use of the case else is optional. Note that the case expression may be a list ofm values separated by commas. For example:

    Select Case fred
       case "1"
          msgbox "fred is a one"
       case "2"
          msgbox "fred is a two"
       case "3"
          msgbox "fred is a three"
       case "4"
          msgbox "fred is a four"
       case "5"
          msgbox "fred is a five"
       case "6","7","8","9","10"
          msgbox "fred is a between six and ten"
       case else
          msgbox "fred is not 1 to 10"
    End Select

     

    Javascript Switch

    An alternative to using elseif is the switch. The general form is:

    switch ( <expression to test>)
    {
       case <expression 1>:
          code when <expression to test> = <expression 1>
          break;
       case <expression 2>:
          code when <expression to test> = <expression 2>
          break;
       case <expression 3>:
          code when <expression to test> = <expression 3>
          break;
       case <expression 4>:
          code when <expression to test> = <expression 4>
          break;
       default:
          code when <expression to test> does not
          equal to any of the other <expressions>
          break;
    }

    The use of the default is optional. Note that the case expression may be a list. For example:

    switch ( fred )
    {
       case "1":
          alert( "fred is a one");
          break;
       case "2":
          alert( "fred is a two");
          break;
       case "3":
          alert( "fred is a three");
          break;
       case "4":
          alert( "fred is a four");
          break;
       case "5":
          alert( "fred is a five");
          break;
       case "6":
       case "7":
       case "8":
       case "9":
       case "10":
          alert( "fred is a between six and ten");
       default:
          alert( "fred is not 1 to 10");
    }

    Note that multiple values for each case are NOT allowed. However, the task can be accomplished by using successive case statements prior to a single statement to be used for all the same outcome cases.