VBScript
VBScript introduction
VBScript basics
VBScript variables
VBScript procedures
VBScript popup boxes
VBScript conditions
VBScript loops
VBScript arrays
VBScript strings
VBScript date & time
VBScript summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

VBScript variables

Variables are a fundamental concept in many computer languages and knowing how to work with them is essential knowledge.

This lesson focuses on:

What is a variable?

A variable is a container which stores information in a computer's memory. The value of a variable can change all throughout a script.

Declaring variables

In VBScript a variable is declared with the Dim statement.

Syntax:

Dim variableName

Example:

<script type="text/vbscript">
Dim title
</script>

Once you declare a variable, you can give it a value.

Example of declaring a variable and then giving it a value:

<script type="text/vbscript">
Dim title
title = "A book to read"
</script>

In the above example a variable named title is declared. On the next line the variable title is assigned the value "A book to read"

NOTE: The Dim statement should be used only once with each variable - during declaration. After a variable has been declared, you can refer to it by its name without the Dim statement, as using the Dim statement to declare variables is optional, but doing so is good convention.

Option explicit

It is possible that within a script you will misspell a variable name and consequently get unexpected results. For example, if you misspell the variable name "title" as "titld", VBScript will automatically generate a variable with the name "titld". You can prevent this from happening by using the option explicit statement. This statement goes all the way at the top of a script, and when you use it, you must use the Dim statement to declare variables.

Example:

<script type="text/vbscript">
option explicit
Dim title
title = "A book to read"
</script>

Naming variables

When naming variables, several rules should be followed:

Printing variables

Variables are printed by including the variable name in a document.write() command. When printing the value of a variable, the variable name should NOT be included in double quotes.

Example:

<html>
<head>
<title>Printing variables</title>
</head>
<body>
<script type="text/vbscript">
Dim numChairs
numChairs = 5
document.write(numChairs)
</script>
</body>
</html>

Output:

5

You can also print variables together with regular text. To do this, use the & symbol to join the text and variable values together.

Example:

<html>
<head>
<title>Printing variables and text together</title>
</head>
<body>
<script type="text/vbscript">
Dim teamName, teamState, numWins, numLosses

teamName = "The Penguins"
teamState = "Connecticut"
numWins = 12
numLosses = 2

document.write("The name of the team is " & teamName)
document.write("<br />The team is from " & teamState)
document.write("<br />" & teamName & " have won " 
& numWins & " games and have lost " & numLosses & " games")
</script>
</body>
</html>

Output:

The name of the team is The Penguins
The team is from Connecticut
The Penguins have won 12 games and have lost 2 games

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments