VBScript Variables


Variables are "containers" for storing information.


实例

Try it Yourself - Examples (Only for IE)

Create and Change Variables
How to create a variable, assign a value to it, and then change its value.

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

Create an Array
An array is used to store a series of related data items. This example demonstrates how to create an array that stores names.


Remember algebra from school?

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 is 11.

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


VBScript Variables

Compared 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 begin with a letter
  • Cannot contain a dot (.)
  • Cannot exceed 255 characters

In VBScript, all variables are associated with typesvariantand can store different types of data.


Declare (Create) VBScript Variables

In VBScript, creating a variable is usually called "declaring" a variable.

You can declare VBScript variables with Dim, Public, or Private statements. As follows:

Dim x
Dim carname

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

You can also declare a variable by using its name in the script. As follows:

carname="Volvo"

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

If you misspell a 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 with Dim, Public, or Private statements.

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

Option Explicit
Dim carname
carname=some value


Assign 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 variable "carname" has the value "Volvo", and the variable "x" has the value "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 subroutine. When you exit the subroutine, the variable also becomes invalid. Such variables are called local variables. You can use local variables with the same name in different subroutines, because each variable is only recognized within the subroutine where it is declared.

If you declare a variable outside 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 the parentheses is 2. The array subscript starts at 0, so the 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, by using the subscript number of a specific array element, you can retrieve the value of any element. As follows:

mother=names(0)

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

Dim table(4,6)

Assign values to a two-dimensional array:

Examples (Only for IE)

<html>
<body>

<script type="text/vbscript">
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("<p>")
for j=0 to 2
document.write(x(i,j) & "<br />")
next
document.write("</p>")
next
</script>

</body>
</html>

Try it Yourself »

Other Extensions