Operators behave like predefined functions that are part of the WMLScript language. For example, the expression (x + y) is composed of the variables x and y combined with the + operator. WMLScript uses the + operator to return the sum of the two operands, x and y.
This section covers the following topics:
An assignment operator is a character that assigns a value to an operand. Assignment operators do not necessarily imply sharing of structure, nor does changing the value assignment of one operand change the binding of any other operand. WMLScript supports assignment with the following operators:
Example uses of assignment operators:
var a = "abc";
var b = a;
b = "def"; // Value of a is "abc"
An arithmetic operator is a character that performs arithmetic operations. WMLScript supports basic, complex, and unary arithmetic operators.
Example uses of arithmetic operators:
var y = 1/3; // y = 0.333
var x = y*3+(++b);
Basic binary arithmetic operations:
| Operator | Operation |
|---|---|
+
|
add (numbers)/concatenation (strings) |
-
|
subtract |
*
|
multiply |
/
|
divide |
div
|
integer division |
Complex binary operations:
Basic unary operations:
Logical operators combine or negate relational expressions.
The basic logical operators are:
The logical AND operator evaluates the first operand and tests the result. If the result is false or invalid, the result of the operation is false or invalid, and the second operand is not evaluated. If the first operand is true, the result of the operation is the result of the second operand.
Similarly, the logical OR evaluates the first operand and tests the result. If the result is true or invalid, the result of the operation is true or invalid, and the second operand is not evaluated. If the first operand is false, the result of the operation is the result of the second operand.
weAgree = (iAmRight && youAreRight) ||
(!iAmRight && !youAreRight);
WMLScript requires a Boolean value for logical operations and supports automatic conversion from other types to Boolean types.
NOTE
If the value of the first operand for logical AND or OR is invalid, the second operand is not evaluated, and the result of the operand is invalid.
var a = (1/0) || foo ();
// result: invalid, no call to foo ()
var b = true || (1/0); // true
var c = false || (1/0); // invalid
String operators concatenate string variables. The + and += operators concatenate the strings. The standard String library supports other string operations.
Example uses of string operators:
var str = "Beginning" + "End";
var chr = String.chartAt (str,10) // chr = "E"
Comparison operators compare values within a variable. Comparison operators are also known as relational operators.
WMLScript supports the following comparison operators:
| Operator | Operation |
|---|---|
<
|
less than |
<=
|
less than or equal |
==
|
equal |
>=
|
greater than or equal |
>
|
greater than |
!=
|
inequality |
Comparison operators follow these rules:
Boolean: true is larger than false
Integer: Comparison is based on the given integer values
Floating-point: Comparison is based on the given floating-point values
String: Comparison is based on the order of character codes of the given string values. Character codes are defined by the character set supported by the WMLScript Interpreter
Invalid: If at least one of the operands is invalid, the result of the comparison is invalid
Example uses of comparison operators:
var res = (myAmount > yourAmount);
var val = (( 1/0) == invalid); // val = invalid
In comparisons, the following hierarchy promotions occur:
WMLScript does not support conventional arrays. However, the standard String library supports functions that mimic the behavior of conventional arrays.
The String library contains functions that create and manage character and string arrays.
The following is an example of a string function:
function dummy() {
var str = "Mary had a little lamb";
var word = String.elementAt (str,4,"");
}
Comma operators combine multiple evaluations into one expression. The result of the comma operator is the value of the second operand.
The following is an example of a comma operator:
for (a=1, b=100; a < 10; a++,b++) {
//... do something ...
}
Commas used in a function call to separate parameters or in a variable declaration to separate multiple variable declarations are not comma operators. In these cases, the comma operator must be placed inside parentheses as shown in the following example:
var a = 2;
var b = 3, c = 3;
myFunction("Name", 3*(b* a, c));
// Two parameters: "Name", 9
Conditional operators assign a value to an expression based on the Boolean result of an initial statement. Conditional operators (?:) are essentially if-then-else statements that take three operands, arranged as follows:
d = operand1 ? operand2 : operand3;
operand1 is the condition being evaluated. If the condition is true, expression d results in the value or result of operand2. If the condition is false or invalid, expression d results in the value or result of operand3.
The following is an example of a conditional operator:
myResult = flag ? "Off" : "On (value=" + level + ")";
The typeof operator returns an integer value that describes the type of the given expression. The typeof operator does not convert the result from one type to another.
typeof returns a value based the data type as shown in the following table:
The following is an example of a typeof operator:
var str = "123";
var mytype = typeof str; // myType = 2
The isvalid operator checks the validity of a variable or expression. The isvalid operator returns a Boolean value of false if the result type of an expression is invalid or if a variable not a valid type. If the result type of an expression is not invalid or if a variable is of a valid type, isvalid returns true. The isvalid operator does not convert the result from one type to another.
The following is an example of an isvalid operator:
var str = "123";
var ok = isvalid str; // true
var tst = isvalid (1/0); // false