HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Classic ASPVariablesStringsConditions (If / Select Case)Loops (For / Do While)Form and Request
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
🔁 Condition & Loop
🧩 Functions & Forms
Classic ASP›Variables

Variables

Variable definition with Dim, assignment, and safe usage with Option Explicit.

Simple enough for a 5-year-old to understand

Define with Dim

Variables in VBScript are defined with Dim. Assignment is done with the equals sign.

Example code
1
Dim name
Defines the variable.
VariableDefines the variable.
2
name = "Alex"
Assigns a value.
VariableAssigns a value.
3
Response.Write name
Alex writes.
Output / InputAlex writes.
VariableOutput / Input
💡 Hint: Types are not specified in VBScript; a variable can hold any kind of value.

The value can change

Example code
1
Dim score
2
score = 10
Initial value.
VariableInitial value.
3
score = 20
New value; the old one is gone.
VariableNew value; the old one is gone.
4
Response.Write score
Writes 20.
Output / InputWrites 20.
VariableOutput / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.With which keyword are variables defined in VBScript?

About this topic

In VBScript, there is only a single data type: the variant type. A variable can hold a number one moment and text the next, and conversions happen automatically most of the time. This flexibility makes fast writing easy while masking errors.

Declaring variables is not mandatory by default. Assigning a value to a non-existent name creates a new variable; this means a typo silently creates an empty variable. Putting the declaration requirement directive on the very first line of the page eliminates this risk.

Objects are not assigned like normal values; a separate assignment keyword is required. Forgetting this leads to assigning the object's default value instead of the object itself, resulting in seemingly unrelated errors.

What you will learn

  • Declaring variables
  • The declaration requirement directive
  • The behavior of the variant type
  • Assigning objects
  • Distinguishing empty value types

Who is this for?

  • Inheriting ASP pages
  • Dealing with silent errors
  • Those not used to typeless languages

When to use?

Every ASP page must have the declaration requirement directive on its first line; this single line is the most effective improvement you can make in a typeless language.

Objects like database connections must be explicitly released after use; otherwise, connections will accumulate on the server.

Common mistakes

Wrong

<%
Dim kullaniciAdi
kullaniciAdi = "ayse"
Response.Write kullaniciAd
%>

The name on the last line contains a typo; since there is no declaration requirement directive, a new and empty variable is created, and the page outputs nothing. There is no error message, making it very hard to find. When the directive is added, the same code throws an error immediately and points out the issue.

Correct

<%
Option Explicit
Dim kullaniciAdi
kullaniciAdi = "ayse"
Response.Write kullaniciAdi
%>

Wrong

<%
Option Explicit
Dim baglanti
baglanti = Server.CreateObject("ADODB.Connection")
%>

Object assignment requires a separate keyword; regular assignment tries to get the object's default property and throws a type mismatch error. This is one of the most common runtime errors in Classic ASP.

Correct

<%
Option Explicit
Dim baglanti
Set baglanti = Server.CreateObject("ADODB.Connection")
%>

Wrong

<%
Option Explicit
Dim adet
adet = "5"
Response.Write adet + 3
%>

The plus operator performs addition if both operands look like numbers, but if one is actual text, it tries concatenation and behaves ambiguously. Here it outputs eight, whereas the intention is ambiguous. You should always use the separate concatenation operator for joining text and explicit conversion for adding numbers.

Correct

<%
Option Explicit
Dim adet
adet = "5"
Response.Write CInt(adet) + 3
%>

Frequently asked questions

Where is the declaration requirement directive written?

At the very beginning of the first code block on the page, before any other statement. If there are included files, it must come even before them; otherwise, the directive is ignored and provides no protection.

What is the difference between empty, null, and nothing?

One represents a variable with no value assigned, one represents an undefined value coming from a database, and one represents an unassigned object reference. All three are checked with different testing functions, and confusing them leads to faulty conditions.

Can I learn the variable type?

Yes, there are functions that report the current subtype of a variable. Checking whether user input is numeric before feeding it into a calculation is a fundamental check you must perform in a typeless language.

Summary

There is a single variable type in VBScript.

The declaration requirement directive is written on the first line.

Typos silently produce empty variables.

Objects are assigned with a separate keyword.

Explicit conversion is required to add numbers.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code