Type Operators
By now, you’ve heard a lot about the different datatypes WMLScript provides and how it converts among these as necessary.
There are occasions where you may want to find the exact datatype of
a value, or at the very least check whether it’s
invalid
or not. (invalid
usually represents some sort of error condition, so this last check
is often equivalent to checking whether some operation went well.)
Fortunately, WMLScript provides operators for testing datatype and validity.
The typeof
operator returns an integer
representing the datatype of its argument. It doesn’t attempt
to do any conversions: the integer indicates the datatype of the
argument.
Because the typeof
operator never attempts to
convert its argument and returns an integer value even when its
argument is invalid
, it never returns an
invalid
value. It always returns an integer.
Table 11-1 gives the five integer values that represent the five WMLScript datatypes.
Table 11-1. Values Given by typeof
Datatype |
typeof Value |
---|---|
Integer |
|
Floating point |
|
String |
|
Boolean |
|
Invalid |
|
Often, the typeof
operator provides more
information than you need. For example, consider a complicated
arithmetic expression. You know that the result is either an integer
or a floating-point value, unless something went wrong with the
evaluation, in which case the result is invalid
.
You could check if everything went well with something like:
if (typeof result != 4) { /* Type not invalid: result was OK. */ ... } else ...
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.