11.7. WMLScript typeof Operator - Finding Data Types of Variables

As mentioned previously in the "WMLScript Data Types" section of this WMLScript tutorial, there is only one type of variable in WMLScript, which is var. A variable can contain a value of any of the five primitive data types -- Boolean, integer, float, string and invalid. If you want to determine the data type of the value stored in a variable, you can make use of the typeof operator. The typeof operator returns an integer that indicates the data type of a value.


WMLScript data type

Return value of the typeof operator

Integer

0

Float

1

String

2

Boolean

3

Invalid

4


The following WMLScript example demonstrates how to use the typeof operator to find the data type of the value stored in a variable:


var number1 = 100;
var number2 = 1.11;
var x = typeof number1;
var y = typeof number2;


After the execution of the above script, the x variable contains the value 0 (indicates that an integer is stored in the number1 variable) while the y variable contains the value 1 (indicates that a floating-point number is stored in the number2 variable).

The operand of the typeof operator does not have to be a variable. For example:


var x = typeof (100 / 0);
var y = typeof (10 / 3);
var z = typeof "WMLScript Tutorial";


After executing the above script, x contains 4 (invalid type), y contains 1 (float type) and z contains 2 (string type).


Previous Page Page 24 of 71 Next Page


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page