VBScript Variables
Variables are "containers" for storing information.
Try It Yourself - Examples (IE Only)
Insert Variable Values into a Text
Remember Algebra from School?
Remember algebra from school? x=5, y=6, z=x+y
Do you recall? A letter (like x) can hold a value (like 5), and you can use this information to calculate that z equals 11.
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).
VBScript Variables
Similar to algebra, VBScript variables are used to hold values or expressions.
Variables can have short names, like x, or more descriptive names, like carname.
VBScript variable naming rules:
- Must start with a letter
- Cannot contain a period (.)
- Cannot exceed 255 characters
In VBScript, all variables are associated with the variant type, which can store different types of data.
Declaring (Creating) VBScript Variables
Creating a variable in VBScript is often referred to as "declaring" a variable.
You can declare VBScript variables with the Dim, Public, or Private statements. Here is an example:
Now you have created two variables. The variable names are "x" and "carname".
You can also declare a variable in a script by simply using its name. Here is an example:
Now you have created another variable. The variable name is "carname". However, this practice is not recommended because you might misspell the variable name in your script, which could lead to unexpected results when the script runs.
If you misspell a variable name, such as "carname" as "carnime", the script will automatically create a new variable called "carnime". To prevent this, you can use the Option Explicit statement. If you use this statement, you must declare all variables with Dim, Public, or Private statements.
Place the Option Explicit statement at the top of your script, like this:
Assigning Values to Variables
You can assign a value to a variable, like this:
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 variable "carname" has the value "Volvo", and the variable "x" has the value "10".
Variable Lifetime
The lifetime of a variable refers to how long it can exist.
When you declare a variable within a subroutine, the variable can only be accessed within that procedure. When you exit the procedure, the variable becomes invalid. Such variables are called local variables. You can use the same name for local variables in different procedures because each variable is only recognized within the procedure where it is declared.
If you declare a variable outside of a subroutine, all subroutines on your page can access it. This type of variable exists from the time it is declared until 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 with 3 elements is declared:
The number shown in the parentheses is 2. Array indexes 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, like this:
Similarly, you can retrieve the value of any element by using its index number, like this:
You can use up to 60 dimensions in an array. To declare a multi-dimensional array, separate the dimensions with commas in the parentheses. Here, we declare a 2-dimensional array with 5 rows and 7 columns:
Assigning values to a 2-dimensional array: