Why does VBScript have Null, Nothing and Empty, and what are the differences between them?
When you declare a variable in VBScript, the variable’s value before the first assignment is undefined, or Empty.
Dim varValue ‘ Empty value
So basically, Empty says I am an uninitialized variant. If you need to detect whether a variable actually is an empty variant and not a string or a number, you can use IsEmpty. Alternatively, you could use TypeName or VarType, but I prefer IsEmpty.
Nothing is similar to Empty but subtly different. Empty says I am an uninitialized variant, Nothing says I am an object reference that refers to no object. Objects are assigned to variables using the Set statement. Since the equality operator on objects checks for equality on the default property of an object, any attempt to say:
If varValue = Nothing Then
is doomed to failure – Nothing does not have a default property, so this will produce a run-time error. To check to see if an object reference is invalid, use:
If varValue Is Nothing Then
Null is more obscure. The semantics of Null are very poorly understood, particularly amongst people who have little experience with programming. Empty says I’m an uninitialized variant, Nothing says I’m an invalid object and Null says I represent a value which is not known. Null is not True, not False, but Null! The correct way to check for Null is much as you’d do for Empty: use IsNull (or TypeName or VarType.)
Nothing vs Empty vs Null
There has always been confusion about the semantics of data that are not even there. Usually they’ve written code something like: