Variables are "containers" for storing information.
 ## Try it Yourself - Example (Only for IE) (#) How to create a variable, assign a value to it, and then change its value. (#) How to insert variable values into a piece of text. (#) Arrays are used to store a series of related data items. This example demonstrates how to create an array that stores names.
## Do you remember algebra from school? Do you remember algebra from school? x=5, y=6, z=x+y Remember? A letter (like x) can hold a value (like 5), and you can use the information above to calculate that z has the value of 11. These letters are called **variables**, variables can be used to store values (x=5) or expressions (z=x+y).
## VBScript Variables Compared to algebra, VBScript variables are used to store values or expressions. Variables can have a short name, like x, or a more descriptive name, like carname. The rules for VBScript variable names: * Must begin with a letter * Cannot contain a period (.) * Cannot exceed 255 characters In VBScript, all variables are of the type _variant_, which can store different types of data.
## Declaring (Creating) VBScript Variables Creating a variable in VBScript usually means "declaring" the variable. You can declare VBScript variables using the Dim, Public, or Private statements. As follows: Dim x Dim carname Now you have created two variables. The variable names are "x" and "carname". You can also declare a variable in your script by using its name. As follows: carname="Volvo" Now you have created another variable. The variable name is "carname". However, this is not a good practice because you may misspell the variable name in your script, which may cause strange results when the script runs. If you misspell the variable name, for example, misspelling "carname" as "carnime", the script will automatically create a new variable named "carnime". To prevent the script from doing this, you can use the Option Explicit statement. If you use this statement, you must declare all variables using the dim, public, or private statements. Place the Option Explicit statement at the top of your script, as follows: Option Explicit Dim carname carname=some value
## Assigning Values to Variables You can assign a value to a variable as follows: carname="Volvo" x=10 The variable name is on the left side of the expression, and the value to be assigned to the variable is on the right side. Now the value of variable "carname" is "Volvo", and the value of variable "x" is "10".
## Variable Lifetime Variable lifetime refers to the duration for which it can exist. When you declare a variable within a subroutine, the variable can only be accessed within that program. When you exit that program, the variable will also become invalid. Such variables are called local variables. You can use the same name for local variables in different subroutines because each variable can only be recognized within the subroutine that declares it. If you declare a variable outside of a subroutine, all subroutines on your page can access it. The lifetime of such variables starts when they are declared and ends when the page is closed.
## VBScript Array Variables Array variables are used to store multiple values in a single variable. In the following example, an array containing 3 elements is declared: Dim names(2) The number shown in parentheses is 2. Array indices start at 0, so this array contains 3 elements. This is a fixed-size array. You can assign data to each element of the array as follows: names(0)="Tove" names(1)="Jani" names(2)="Stale" Similarly, you can retrieve the value of any element by using its index number in the array. As follows: mother=names(0) You can use up to 60 dimensions in an array. The method of declaring a multi-dimensional array is to separate numbers with commas in parentheses. Here, we declare a 2-dimensional array containing 5 rows and 7 columns: Dim table(4,6) Assigning values to a 2-dimensional array: ## Example (Only for IE) Dim x(2,2) x(0,0)="Volvo" x(0,1)="BMW" x(0,2)="Ford" x(1,0)="Apple" x(1,1)="Orange" x(1,2)="Banana" x(2,0)="Coke" x(2,1)="Pepsi" x(2,2)="Sprite" for i=0 to 2 document.write("
")
for j=0 to 2
document.write(x(i,j) & "
")
next
document.write("
YouTip