Latest web development tutorials

VBScript variable

Variable is a "container" for storing information.


Try - Example (only for IE)

Creating and changing variables
How to create a variable and assign to it, and then change its value.

Insert variable values in a text
How to insert variable values ​​in some text.

Creating an array
The array is used to store a series of related items. This example demonstrates how to create a storage array name.

Remember in school algebra you learned?

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

do you remember? A letter (such as x) can save a value (such as 5), and can use the information above to calculate the value of z is 11.

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

VBScript variable

Compared with algebraic, VBScript variable used to hold values ​​or expressions.

Variable can have a short name, like x, or a more descriptive name, such as carname.

VBScript variable name rule:

  • We must begin with a letter
  • Not contain a dot (.)
  • Not more than 255 characters

In VBScript, all variables are associated with the type ofvariantcan store different types of data.

Statement (Creating) VBScript variable

Create the VBScript variable is usually referred to as "declaration" variable.

You can Dim, Public or Private statement to declare VBScript variables. As follows:

Dim x
Dim carname

Now that you have created two variables. It is the name of the variable "x" and "carname".

You can also use it in the script name to declare variables. As follows:


Now you've created a variable. Name of the variable is "carname". Then, this approach is not a good idea since you may misspell the variable name in the script, that can cause strange results when the script runs.

If you misspell a variable name, such as "carname" variable is misspelled as "carnime", the script automatically creates a new variable named "carnime" of. In order to prevent a script to do this, you can use the Option Explicit statement. If you use this statement, you must use the dim, public or private statement to declare all the variables.

To the top of the Option Explicit statement is placed in the script, as follows:

Option Explicit
Dim carname
carname=some value

Assign values ​​to variables

You can assign a variable as follows:


Variable names are on the left side of the expression, you need to assign the value of the variable on the right side in the expression. Variable "carname" The value is now "Volvo", the variable "x" is the value of "10."

Lifetime of variables

Live variable refers to the length of time it can exist.

When you declare a variable in a subroutine, variables can only be accessed within the program. When you exit the program, the variable will fail. Such variables are called local variables. You can use the same name as a local variable in a different routine, because each variable declaration can only be identified within its program.

If you declare a variable outside of a subroutine, all routines on your page can access it. Survival of these variables begins they are declared, and ends the page is closed.

VBScript array variable

Variable is used in a single array of a plurality of values ​​stored in the variable.

In the following example, we declare an array contains three elements:

Dim names(2)

Figures in parentheses show the 2. Array indices start at 0, so the array contains three elements. It is the capacity of a fixed array. You can assign each element of the data array, as follows:


Similarly, by using the following labels are particular array element, you can retrieve the value of any element. As follows:


You can use up to 60 in one dimension array. The method of multidimensional array is in parentheses with commas to separate numbers. Here, we declare a two-dimensional array contains five rows of seven:

Dim table(4,6)

For the two-digit group assignment:

Examples (only for IE)


<script type="text/vbscript">
Dim x(2,2)
for i=0 to 2
for j=0 to 2
document.write(x(i,j) & "<br />")


try it"