Assignment Operators

Having expressions isn’t much use unless you can assign them to variables, and assigning to variables is the purpose of the assignment operators. The left side of any assignment operator must be the variable to assign.

The most common assignment operator is the simplest, which simply assigns an expression to a variable. It’s represented by =.

WMLScript also provides assignment operators that combine some other operator with the assignment. For example:

x += y

is a short form of:

x = (x + y)

There are 12 operators, each combining one of the simple binary operators with assignment: +=, -=, *=, /=, div=, %=, &=, |=, ^=, <<=, >>=, and >>>=. All the normal rules for each operator apply; so, for example, += can do string concatenation as well as addition, and /= always gives a floating-point result. Note that there are no assignment operators for the Boolean or comparison operators: if you really need that behavior, you have to write it out in full. (You probably won’t need it, though.)

The result of any assignment operation is the value that was assigned. For example:

foo (a = b)

sets a to the contents of b and then calls foo( ) with the new value of a. In the same way:

foo (a *= b)

multiplies a by the contents of b, then calls foo( ) with the new value of a.

Get Learning WML, and WMLScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.