Learn about VBScript data types, including Integer, String, Boolean, and more. Understand how to declare and use them effectively with practical examples.
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 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.
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.
numeric_types.vbs
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.
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.
string_type.vbs
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.
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.
boolean_type.vbs
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.
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.
date_type.vbs
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.
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.
special_types.vbs
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.
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
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.
List all VBScript tutorials.