VBScript – Variables and Data Types

Variable is a name given to a data storage location such as memory that can change during program execution.  It is a place in the memory where the data can be placed and retrieved during the course of execution.

VBScript has only one data type that is Variant.  VBScript variables (Variant) can contain either string or numeric value. When a number is assigned to the variable it behaves as numeric and as a string when assigned a string value. There is no need to declare the type of variable before using it as in standard programming languages such as C, C++, Java, etc but if declared that makes the code neat and readable.

Variant has different subtypes such as:

  1. Empty  – Not initialized
  2. Null – Contains no data
  3. Boolean – Value is either True or False
  4. Byte – Contains integer value from 0 to 255
  5. Integer – Holds integer value in the range of –32768 to +32767
  6. Currency
  7. Long 
  8. Single – Single precision floating point number
  9. Double – Double precision floating point number
  10. Date / Time – Contains data / time
  11. String – Variable length string
  12. Object – Holds an object
  13. Error – Holds error

Though above are all the subtypes of variant there is no need to declare in the beginning and use them in the script.

Let us try to write a vbscript and understand the concept.

x=5647
y=-100
sum=x+y
MsgBox sum

If (sum>0) Then
  res="positive"
  Else
  res="negative"
End If
MsgBox res  

In the above code although x,y,z, and sum are not declared as Integer data type VBScript treated them as integers and assigned appropriate subtype.  Also, after assigning string value to res variable that has taken the role of String variable. This makes the users of VBScript more comfortable and concentrate on the actual task of writing the script.

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.