Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

VBScript Variables

VBScript Variables


Variables are "containers" for storing information.


Try it - Examples (IE only)

Creating and Changing Variables
How to create a variable, assign it a value, and then change its value.

Inserting Variable Values into Text
How to insert a variable value into a piece of text.

Creating an Array
Arrays are used to store a series of related data items. This example shows how to create an array that stores names.


Remember the algebra you learned in school?

Remember the algebra you learned in school? x=5, y=6, z=x+y

Remember? A letter (such as x) can hold a value (such as 5), and using the information above you can calculate that z is 11.

These letters are calledVariablesvariables, which can be used to hold values (x=5) or expressions (z=x+y).


VBScript Variables

Compared with algebra, VBScript variables are used to hold values or expressions.

A variable can have a short name, such as x, or a more descriptive name, such as carname.

Rules for VBScript variable names:

In VBScript, all variables are associated with a type variant and can store different types of data.


Declaring (Creating) VBScript Variables

Creating a variable in VBScript usually refers to "declaring" the variable.

You can declare VBScript variables with the Dim, Public, or Private statement, as shown below:

Dim x

Dim carname

Now you have created two variables. The variable names are "x" and "carname".

You can also declare a variable in a script simply by using its name, as shown below:

carname="Volvo"

Now you have created another variable named "carname". However, this is not a good practice, because you may misspell a variable name in your script, which may cause strange results when the script runs.

If you misspell a variable name, for example spelling "carname" as "carnime", the script will automatically create a new variable named "carnime". To prevent this, you can use the Option Explicit statement. If you use this statement, you must declare all variables with the dim, public, or private statement.

Place the Option Explicit statement at the top of the script, as shown below:

Option Explicit

Dim carname

carname=some value


Assigning Values to Variables

You can assign a value to a variable, as shown below:

carname="Volvo"

x=10

The variable name is on the left side of the expression, and the value to be assigned is on the right side. Now the value of the variable "carname" is "Volvo", and the value of the variable "x" is "10".


The lifetime of variables

The lifetime of a variable refers to how long it can exist.

When you declare a variable inside a subroutine, the variable can only be accessed within that procedure. When the procedure exits, the variable becomes invalid. Such variables are called local variables. You can use local variables with the same name in different subroutines, because each variable is recognized only within the procedure in which it is declared.

If you declare a variable outside a subroutine, all subroutines on your page can access it. The lifetime of such variables begins 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 the parentheses is 2. Array subscripts 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 shown below:

names(0)="Tove"

names(1)="Jani"

names(2)="Stale"

Similarly, by using the subscript of a specific array element, you can retrieve the value of any element, as shown below:

mother=names(0)

You can use up to 60 dimensions in an array. To declare a multidimensional array, separate the numbers with commas inside the parentheses. Here we declare a 2-dimensional array with 5 rows and 7 columns:

Dim table(4,6)

Assigning Values to a Two-Dimensional Array:

Example (for IE only)

Try it »