Condition Testing |
Active Web |
Some of the script statements require a condition. This condition is a boolean value (true or false) and is derived from a condition test.
A condition test can use a combination of Comparison operators and Logical operators.
The Comparison operators compare the values of objects and this comparison produces a boolean result. The Logical operators combine booleans to produce a single boolean result that is the result of the condition test.
e.g.
(a == b) Result is true if the value of a is the same as the value of b.
(a > b && c <= d) Result is true if the value of a is greater than the value of b and the value of c is less than or the same as the value of d.
(a != b || c >= d) Result is true if the value of a is not the same as the value of b or the value of c is greater than or the same as the value of d.
You can use brackets in a condition test and this is often done if the condition is large to make it clearer.
e.g. ((a < b) && (a != c || d == e) && (a > d))
When a condition test involves combining two (or more) conditions using the AND logical operator (&&) then the second condition is not tested if the first condition fails, as a failure in the first condition means that the whole condition has failed. This can be useful in preventing runtime errors. For example, calling a function on a null object causes a runtime error but if we test the object for null first then the call is never made if the object is null.
e.g. (myString != null && myString.indexOf("a") == 0)
You can use an object as a boolean result. If the object is specified on its own without a comparison operator then the result will be the boolean value of the object. The boolean value is derived using the following rules:
Return false if the object does not exist.
Return false if the object is a null primitive.
Return false if the object is a number with a value of zero.
Return false if the object is a string primitive with a length of zero.
If the object is a boolean object then return its boolean value.
Return true for all other objects.
e.g.
var a;
if(a) Result is false as a is a null primitive.
var a = false:
if(a) Result is false because a is a boolean object set to false.
var a = "";
if(a) Result is false because it is a string primitive with a length of zero.
var a = new String("");
if(a) Result is true because it is a string object (as it is not a primitive we ignore the length).
The following table shows how two objects will compare with all the possible types of values. The 'Object' column does not compare true with anything even another object, this is because objects, by default, only compare true with another reference to the same object.
|
null |
zero (0) |
zero string ("0") |
floating point zero (0.00) |
empty string ("") |
minus one |
plus one (1) |
boolean true |
boolean false |
plus zero (+0) |
minus zero |
Object |
undef-ined |
null |
true |
false |
false |
false |
false |
false |
false |
false |
false |
false |
false |
false |
true |
zero |
false |
true |
true |
true |
true |
false |
false |
false |
true |
true |
true |
false |
true |
zero string ("0") |
false |
true |
true |
true |
false |
false |
false |
false |
true |
true |
true |
false |
false |
floating point zero (0.00) |
false |
true |
true |
true |
true |
false |
false |
false |
true |
true |
true |
false |
true |
empty string ("") |
false |
true |
false |
true |
true |
false |
false |
false |
true |
true |
true |
false |
false |
minus one |
false |
false |
false |
false |
false |
true |
false |
false |
false |
false |
false |
false |
false |
plus one (1) |
false |
false |
false |
false |
false |
false |
true |
true |
false |
false |
false |
false |
false |
boolean true |
false |
false |
false |
false |
false |
false |
true |
true |
false |
false |
false |
false |
false |
boolean false |
false |
true |
true |
true |
true |
false |
false |
false |
true |
true |
true |
false |
false |
plus zero (+0) |
false |
true |
true |
true |
true |
false |
false |
false |
true |
true |
true |
false |
true |
minus zero |
false |
true |
true |
true |
true |
false |
false |
false |
true |
true |
true |
false |
true |
Object |
false |
false |
false |
false |
false |
false |
false |
false |
false |
false |
false |
false |
false |
undef-ined |
true |
true |
false |
true |
false |
false |
false |
false |
false |
true |
true |
false |
true |
Topic ID: 150041