ASP Variables

In classic ASP, variables are declared with Dim and are always of the untyped Variant data type, which can hold numbers, strings, dates, or objects depending on what's assigned.

Declaring Variables with Dim

A variable is created with the Dim keyword followed by its name. You can declare several variables in one Dim statement by separating them with commas. VBScript doesn't require you to declare a variable before using it, but doing so is good practice — adding Option Explicit at the very top of a script forces every variable to be declared, which catches typos that would otherwise silently create a new, unintended variable.

Example

<%
  Option Explicit
  Dim firstName, age
  firstName = "Alex"
  age = 29
  Response.Write(firstName & " is " & age & " years old.")
%>

The Variant Data Type and Naming Rules

Every variable you declare with Dim is a Variant — VBScript's only data type. Unlike languages that require you to pick Integer, String, or Boolean up front, a Variant automatically adapts to whatever value it's holding. The same variable can hold a number at one point in a script and a string later on; VBScript converts between them as needed when you use operators like &.

  • Empty — a variable that's been declared but not yet assigned a value
  • Null — explicitly contains no valid data
  • Boolean — True or False
  • Integer / Double — whole or decimal numbers
  • String — text, always wrapped in double quotes
  • Date — a calendar date and/or time value
  • Object — a reference to an object, such as a database connection
  • A variable name must begin with a letter (a-z or A-Z)
  • It cannot contain a period, space, or most punctuation — underscores are allowed
  • It cannot exceed 255 characters
  • It cannot be a VBScript reserved word, such as Dim, If, or Function

Variable Scope

A variable declared with Dim inside a Sub or Function only exists while that procedure runs and disappears afterward — it's local. A variable declared with Dim at the top level of a script, outside any procedure, is a page-level variable and can be read or changed by any Sub or Function in that same page. For data that needs to survive across multiple pages or requests, ASP also provides the Session("name") and Application("name") collections, which behave like variables but persist for a visitor's session or for the whole application respectively.

Example

<%
  Dim pageMessage
  pageMessage = "Set at the page level"

  Sub ShowMessage()
    Dim localNote
    localNote = "Only visible inside this Sub"
    Response.Write(pageMessage & " / " & localNote)
  End Sub

  ShowMessage()
%>
FunctionWhat It Tells You
VarType(x)A numeric code identifying x's current Variant subtype
TypeName(x)The subtype name as text, e.g. "String" or "Integer"
IsNumeric(x)True if x can be treated as a number
IsDate(x)True if x can be treated as a valid date
Note: Turn on Option Explicit while you're learning — it forces every variable to go through Dim first, so a misspelled variable name raises a clear error instead of quietly creating a fresh, empty variable.

Exercise: ASP Syntax

Which delimiters enclose a server-side script block in classic ASP?