An array stores several related values under one variable name. In VBScript, array indexes are zero-based by default, so Dim myArray(1) creates two elements: myArray(0) and myArray(1).
Dim myArray(1)
Assign values by index:
myArray(0) = "UK"
myArray(1) = "USA"
Writing beyond the declared upper bound raises a subscript error. If the number of elements must change while the script runs, declare a dynamic array and size it with ReDim.
Dim myArray() 'Declare a dynamic array
ReDim myArray(1) 'Allocate indexes 0 and 1
myArray(0) = "UK"
myArray(1) = "USA"
A fixed-size array cannot be resized with ReDim:
Dim myArray(1) 'Fixed-size array
ReDim myArray(2) 'Raises an error
Dim myArray() 'Dynamic array
ReDim myArray(1)
myArray(0) = "UK"
myArray(1) = "USA"
ReDim Preserve myArray(3)
myArray(2) = "Canada"
myArray(3) = "UAE"
For Each item In myArray
Response.Write item & "<br>"
Next
ReDim Preserve keeps existing values while changing the upper bound. For multi-dimensional arrays, preserving values has additional restrictions, so plan the array dimensions before resizing.
| Topic | Use |
|---|---|
| UBound() and LBound() | Find bounds and calculate the number of elements. |
| Split() | Create an array by breaking a string. |
| Join() | Combine array elements into one string. |
| Filter() | Return array elements that include or exclude matching text. |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.