Add item to array in VBScript

Choy picture Choy · Jan 5, 2011 · Viewed 138.4k times · Source

How do you add an item to an existing array in VBScript?

Is there a VBScript equivalent to the push function in Javascript?

i.e.

myArray has three items, "Apples", "Oranges", and "Bananas" and I want to add "Watermelons" to the end of the array.

Answer

Frédéric Hamidi picture Frédéric Hamidi · Jan 5, 2011

Arrays are not very dynamic in VBScript. You'll have to use the ReDim Preserve statement to grow the existing array so it can accommodate an extra item:

ReDim Preserve yourArray(UBound(yourArray) + 1)
yourArray(UBound(yourArray)) = "Watermelons"