Using Empty vs “” to define or test a variable in VBscript

is-emptyvbscript

When declaring a variable to blank (before a loop for example), it is sometimes done as "" or Empty. Also, when checking the value, it is sometimes used with "(Not IsEmpty(variable))" and "variable <> Empty". Is it better to use one vs another and can it cause any issues using it the wrong way?

Ex1:

  • fileNameDate = Empty
  • fileNameDate = ""

Ex2:

  • If (Not IsEmpty(fileNameDate)) Then
  • If fileNameDate <> Empty Then
  • If fileNameDate <> "" Then

Thanks!

————Update————-

Note that my question is not asking for the difference between Null, Empty, and Nothing. I'm simply concentrating on the "Empty" part and whether it's the same things as writing "". For the most part, I have received similar results when interchanging the two, but I dont know if it's just the examples I used. For example, the following confused me some.

My code:

 Dim x, y, z

'Option1 (Do not set x to anything)

'Option2
'x = Empty

'Option3
'x = ""

If x = "" Then
'Action1
End if

If x = Empty Then
'Action2
End if

If IsEmpty(x) Then
'Action3
End if
  • If I go with Option1 and just not define "x", all three actions will occur.
  • If I go with Option2 and set x = Empty, all three actions will also occur
  • But if I go with Option3 and set x = "", only Action1 and Action2 occurs.

Why?

Best Answer

Some considerations:

fileNameDate = Empty  ' The same as just declaring Dim fileNameDate
IsEmpty(fileNameDate) ' = True

Is not the same as:

fileNameDate = ""
IsEmpty(fileNameDate) ' = False

I think the function IsEmpty()is misnamed, because it checks if the variable has been initialized, not if it's actually empty.