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


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