VBScript Variables

Example

Creating a variable
Variables are used to store information. This example demonstrates how to create a variable and assign a value to it.
Inserting a variable value into a text
This example demonstrates how to insert a variable value into a text.
Creating an array
Arrays are used to store a series of related data items. This example demonstrates how to create an array to store names. (We use "for loop" to demonstrate how to output names.)

What is a variable?

Variables are "containers" that can store information. In scripts, the values of variables can be changed. You can view or modify the value of a variable by referencing its name. In VBScript, all variables are type-related and can store different types of data.

Rules for variable names:

  • Must start with a letter
  • Cannot contain a period (.)
  • Cannot exceed 255 characters

Variable declaration

You can declare variables using the Dim, Public, or Private statements, for example:

dim name
name=some value

Now, you have created a variable. The variable name is "name".

You can also create a variable by using its name. For example, like this:

name=some value

In this way, you also create a variable named "name".

However, such practices later on are not good habits, because you might spell the variable name incorrectly in the script, which might cause strange results when the script runs. For example, if you spell the "name" variable as "nime" by mistake, the script will automatically create a variable named "nime". 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 the script like this:

option explicit
dim name
name=some value

Assigning a value to a variable

You can assign a value to a variable like this:

name="George"
i=300 

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

The lifetime of a variable

The lifetime of a variable refers to the duration it can exist.

When you declare a variable within a subroutine, the variable can only be accessed within this program. When you exit the program, the variable also becomes invalid. Such variables are called local variables. You can use the same-named local variables in different subroutines because each variable can only be identified within the program it is declared in.

If you declare a variable outside of a subroutine, all subroutines on your page can access it. This type of variable's lifetime starts when it is declared and ends when the page is closed.

Array Variable

Sometimes, you need to assign multiple values to a single variable. In that case, you can create a variable that can contain a series of values. This type of variable is called an array. The declaration of an array variable uses the variable name followed by a parenthesis(). In the following example, an array containing three elements is created:

dim names(2)

The number in the parentheses is 2. The array index starts at 0 because this array contains three elements. This is a fixed-size array. You can assign data to each element of the array:

names(0)="George"
names(1)="John"
names(2)="Thomas" 

Similarly, by using the index number of a specific array element, we can also retrieve the value of any element. For example:

father=names(0)

You can use up to 60 dimensions in an array. The method to declare a multidimensional array is to separate the numbers with commas inside the parentheses. For example, we declared a two-dimensional array containing 5 rows and 7 columns:

dim table(4, 6)