7. WMLScript Variables
7.1. Declaring WMLScript Variables Using the var Keyword
A variable is used to store some data. You can modify or read the value of a variable during execution. The var keyword is used to declare WMLScript variables. It should be used in the following form (the part enclosed within brackets [] is optional):
var variable_name [= value_to_be_initialized];
Below is an example. The following line of code declares a variable called wmlscript_variable and initializes its value to "Welcome to our WMLScript tutorial".
var wmlscript_variable = "Welcome to our WMLScript tutorial";
Variable initialization is optional. If you do not initialize a variable, the WMLScript interpreter will assign an empty string to it automatically, i.e. the following line of script:
var wmlscript_variable;
is equivalent to:
var wmlscript_variable = "";
Re-declaration of a variable is not allowed in WMLScript. It will result in error.
You can use the var keyword once, but declare more than one variable. For example, the following WMLScript code:
var
wmlscript_variable1;
var wmlscript_variable2;
var
wmlscript_variable3;
is equivalent to:
var wmlscript_variable1, wmlscript_variable2, wmlscript_variable3;
Note that in WMLScript, you must use the var keyword to declare a variable before it can be used. This is different from JavaScript in which automatic declaration of variables is supported. For example, the following function is NOT valid in WMLScript since the wmlscript_variable variable has not been declared:
function
wmlscript_func()
{
wmlscript_variable = "Welcome to our
WMLScript tutorial";
}
WMLScript does not support global variables. Global variables are variables that can be accessed from any functions. This is different from JavaScript in which global variables are supported. For example, the following script is valid in JavaScript but not in WMLScript:
var
wmlscript_variable;
function
wmlscript_func1()
{
wmlscript_variable = "Hello";
}
function
wmlscript_func2()
{
wmlscript_variable = "Welcome to our
WMLScript tutorial";
}
Previous Page | Page 11 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