VBScript Data Types
last modified April 4, 2025
VBScript is a loosely typed language with a variety of data types. Unlike
strongly typed languages, VBScript has only one fundamental data type called
Variant
. The Variant
type can contain different kinds
of data depending on context. This tutorial covers VBScript data types with
practical examples.
VBScript Data Types Overview
VBScript uses Variant
as its primary data type, which can hold
various subtypes. These subtypes are automatically determined based on the
context of usage. The main subtypes include Empty
,
Null
, Boolean
, Byte
,
Integer
, Long
, Single
, Double
,
Currency
, Date
, String
, and
Object
.
The Variant
type is flexible but requires careful handling to avoid
unexpected type conversions. Understanding these subtypes helps write more
reliable scripts. We'll explore each with practical examples in the following
sections.
Numeric Data Types
VBScript supports several numeric subtypes: Integer, Long, Single, Double, and Currency. Integer and Long store whole numbers, while Single and Double store floating-point numbers. Currency is for financial calculations.
Dim age, population, temperature, price age = 25 ' Integer population = 7896541230 ' Long temperature = 98.6 ' Single price = 19.99 ' Currency WScript.Echo "Age: " & age WScript.Echo "Population: " & population WScript.Echo "Temperature: " & temperature WScript.Echo "Price: " & price
This example demonstrates different numeric subtypes. The values are assigned without explicit type declaration. VBScript automatically determines the appropriate subtype based on the value's range and precision requirements.
String Data Type
The String subtype contains sequences of characters. Strings are enclosed in double quotes. VBScript strings can include letters, numbers, and special characters. String manipulation is common in scripting tasks.
Dim name, greeting, address name = "John Smith" greeting = "Hello, World!" address = "123 Main St, Anytown" WScript.Echo name WScript.Echo greeting WScript.Echo "Address: " & address
This example shows string variable declarations and concatenation. The ampersand (&) operator joins strings. Notice how strings can be used alone or combined with other strings in the output.
Boolean Data Type
The Boolean subtype holds logical values: True
or
False
. Booleans are used in conditional statements and logical
operations. In VBScript, True
equals -1 and False
equals 0 when converted to numbers.
Dim isActive, hasPermission, isValid isActive = True hasPermission = False isValid = (10 > 5) ' Expression evaluates to True WScript.Echo "isActive: " & isActive WScript.Echo "hasPermission: " & hasPermission WScript.Echo "isValid: " & isValid
This example demonstrates Boolean variables and expressions. The third variable shows how comparison operations return Boolean values. These are fundamental for control flow in scripts.
Date Data Type
The Date
subtype stores date and time information. VBScript
provides functions like Date
, Time
, and
Now
to work with dates. Dates are enclosed in hash symbols (#) when
assigned directly.
Dim today, currentTime, birthday, appointment today = Date() currentTime = Time() birthday = #12-15-1990# appointment = #3/22/2025 2:30:00 PM# WScript.Echo "Today: " & today WScript.Echo "Current time: " & currentTime WScript.Echo "Birthday: " & birthday WScript.Echo "Appointment: " & appointment
This example shows different ways to work with dates. Notice the various date formats that VBScript accepts. Date handling is crucial for many automation tasks like file operations and scheduling.
Special Data Types - Empty and Null
Empty
indicates an uninitialized variable, while Null
represents no valid data. Empty
variables convert to 0 or ""
depending on context. Null
is used to indicate missing or unknown
data in database operations.
Dim uninitialized, missingData missingData = Null WScript.Echo "TypeName(uninitialized): " & TypeName(uninitialized) WScript.Echo "TypeName(missingData): " & TypeName(missingData) WScript.Echo "IsEmpty(uninitialized): " & IsEmpty(uninitialized) WScript.Echo "IsNull(missingData): " & IsNull(missingData)
This example demonstrates Empty and Null values. The TypeName function reveals the subtype, while IsEmpty and IsNull functions test for these special values. Understanding these is important for robust script error handling.
Source
VBScript Data Types Documentation
In this article, we have explored the fundamentals of VBScript data types, delving into their significance and practical applications. From understanding basic types like Integer, String, and Boolean to seeing how they are used in real-world examples, we have covered essential concepts to help you work effectively with VBScript. With this knowledge, you are now equipped to handle variables and data types confidently in your scripting projects
Author
List all VBScript tutorials.