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 |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data