Variables form the basis of programming. Variables are used to hold value or an expression. Whenever you have a piece of data to work with, you will have to declare a variable.
For example, if you have to store names of students or salaries of employees, you will be using variables named students or salaries.
Variables can also be used for holding expressions. Suppose you have stored the marks of a student in English and Mathematics using the variables markE and markM.
You want to find the total marks. Then, you can use a variable named markT and set its value to markE + markM. In other words, markT = markE + markM. Here, markT is a variable which holds an expression.
In this tutorial, you will learn-
Declaring variables is the same as creating variables because you are instructing the computer to reserve memory space. You can name the variable the way you want. It can be short names like x, y or z or more self-describing names like student, Name, salary etc. Providing clear and meaningful names to variables is considered a good programming practice.
There are certain rules for VBScript variable names.
For declaring variables, you need to use the keyword Dim. Suppose you plan to use a variable named “salary” in your VBScript program, syntax
Dim salary;
Just declaring the VBS variables will not help you, use it. You will have to assign a value to it at some point or another and this process is known as initializing the variable. If you are planning to declare a variably named salary, then you can code like this:
Dim salary salary = 10000
The important thing you need to make sure is that you should not assign a value to the variable as and when you declare it. Suppose you write a statement like this:
Dim salary = 10000
If you try to output salary using document.write, it will not return any output.
Code Example
Step 1) Open your text editor and add the following lines of code.
<html> <head> <title>Variables</title> </head> <body> <script type="text/vbscript"> Dim variable1 variable1="John" document.write(variable1) ‘Dim variable2 = "Smith" ‘document.write(variable2) </script> </body> </html>
Step 2) Save this file as variable.html in your preferred location and then open this in IE (following the steps specified in the previous chapter). Now, you will see the value John on the browser.
Step 3) Next, uncomment line # 11 & 12
Again save the file and refresh the IE browser if it is already opened or open the file in the IE browser. You might be wondered to see nothing; neither John nor Smith. The problem here is that you tried to assign the value to the variable while declaring it which is not allowed.
VBScript provides you the freedom to use variables without declaring it (called loose binding). For example,without having the statement Dim student, you can assign a value to the variable student like – student = "John"
But, it is not at all a good programming practice. If you use a variable without declaring it and misspell the same variable when you use it again, VBScript will not prompt you of the error.
So to make the code easier to read and to identify the errors, you should use the Option Explicit statement at the beginning of your code so that you will be forced to declare all your variables even if you forget to do so. To avoid variable type related issues, it is always good to specify the statement Option Explicit at the beginning of your VBScript code.
Code Example:
Step 1) Open your text editor and add the following lines of code.
<html> <body> <script type="text/vbscript"> Option Explicit ‘Dim markE, markM, markT markE=90 markM=86 markT=markE+markM document.write("Your marks in English is " & markE & "." & "<br />") document.write("Your marks in Mathematics is " & markM & "." & "<br />") document.write("Your total marks is " & markT & ".") </script> </body> </html>
Step 2) Save the file as variables.html in your preferred location. Now open the file in Internet Explorer and your screen is blank. Why ? because you have used option explicit but not declared variables before using them
Step 3) Now to understand the importance of Option Explicit statement, uncomment Line 5 in the above code
Step 4) Save the variables.html file and refresh your browser. Now, your output will be like this:
Note - To concatenate two strings, you need to use “&”. In above example, its used inside document.write command. It is obvious that the calculation of total marks is wrong. Now just add the first statement Option Explicit at the beginning of VBScript code (without the Dim statement).
Save the file and see the output. You will get nothing as output which indicates that your code has some error. Here the error is you have not declared variables before using it even after specifying Option Explicit statement.
You can also declare variables using public and private keywords like a public student or private student. But, you have to be more careful while using these two keywords for declaring variables because it will change the scope of your variables.
You can also store multiple values in a single variable and such variables are known as VBScript array variables. Suppose, you want to store details like name, marks, address etc of 30 students. It will be really difficult to create and manage sets of 30 variables for names, marks, addresses and so on.
Instead, you can declare a single variable named students and store the names of all 30 students in this variable. In such case, you will declare the variable as Dim students(29) (array index starts from zero) and you will assign values as
students(0) = "John" students(1) = "Hannah" students(2) = "Kevin" ....... ....... students(28) = "Rose" students(29) = "Emma"
Similarly, you can create variables like marks, address etc to store the respective values of all 30 students. You can also create multidimensional arrays having up to 60 dimensions.
Code Example:
Open your text editor and add the following lines of code.
<html> <body> <script type="text/vbscript"> Option Explicit Dim students(19), marks(19) students(0) = "John" marks(0) = 95 students(1) = "Emma" marks(1) = "83" students(2) = "Kevin" marks(2) = 87 document.write(students(0) & " has scored " & marks(0) & ".<br />") document.write(students(1) & " has scored " & marks(1) & ".<br />") document.write(students(2) & " has scored " & marks(2) & ".<br />") </script> </body> </html>
Here, we have stored details of only three students. You can add details of up to 20 students as we have set the size of the array as 20 (as index starts from 0).
In the previous section, you might have noticed that we assigned different types of data to the variables.We have stored numbers (mark and salary), strings (name) etc in different variables.
These numbers, strings etc are known as data types. In fact, VBScript has only one data type called Variant. A variant is a special kind of data type which can hold different kinds of information.
If you use Variant in a numeric context, it behaves like a number and when you use it in a string context, it behaves as a string.
In other words, when you specify salary=10000, VBScript assumes that salary is a numeric data type. A Variant makes specific distinctions about the nature of the data. For example, you can use variant type to store Boolean values, currency, date and so on.
These different categories of information that can be contained in a Variant are called subtypes. Though most of the time, Variant behaves in such a way that is most appropriate for the data it contains, you should be aware of different subtypes.
Following is the list of VBScript Data Types.
There are two built-in VBScript functions that help you know the subtype of a variable: “varType()” and “typeName()”.
The var type returns the numeric representation and typeName() returns the text representation of the subtype of the variable. Each subtype has a predefined numeric representation.
Code Example
Open your text editor and add the following lines of code.
<html> <head> <script type="text/vbscript"> Option Explicit Dim a a = Empty document.write("a = " & a & "<br />") document.write("The numeric representation of a is " & VarType(a) & "<br />") document.write("The variable a is of " & TypeName(a) & " data type." & "<br /><br />") Dim b b = Null document.write("b = " & b & "<br />") document.write("The numeric representation of b is " & VarType(b) & "<br />") document.write("The variable b is of " & TypeName(b) & " data type." & "<br /><br />") Dim c c = 4 document.write("c = " & c & "<br />") document.write("The numeric representation of c is " & VarType(c) & "<br />") document.write("The variable c is of " & TypeName(c) & " data type." & "<br /><br />") Dim d d = -2100483648 document.write("d = " & d & "<br />") document.write("The numeric representation of d is " & VarType(d) & "<br />") document.write("The variable d is of " & TypeName(d) & " data type." & "<br /><br />") Dim e e = -3.402823E38 document.write("e = " & e & "<br />") document.write("The numeric representation of e is " & VarType(e) & "<br />") document.write("The variable e is of " & TypeName(e) & " data type." & "<br /><br />") Dim f f = "John" document.write("f = " & f & "<br />") document.write("The numeric representation of f is " & VarType(f) & "<br />") document.write("The variable f is of " & TypeName(f) & " data type." & "<br /><br />") Dim g g = True document.write("g = " & g & "<br />") document.write("The numeric representation of g is " & VarType(g) & "<br />") document.write("The variable g is of " & TypeName(g) & " data type." & "<br /><br />") </script> </head> <body> </body> </html>
Save the file as subtype.html and open it in IE. Your output will look like this:
NOTE: You can also declare variables using public and private keywords like public student or private student. But, you have to be more careful while using these two keywords for declaring variables because it will change the scope of your variables.
In case you see a blank page after you run the code, do the following
<meta http-equiv="x-ua-compatible" content="IE=10">