# Tips for golfing in VBScript

What general tips do you have for golfing in VBScript? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to VBScript. Please post one tip per answer.

Thanks to Marcog for the original idea. :)

• My friends would like this :) – ckjbgames Mar 29 '17 at 1:41
• Just in case anyone is editing vbs in notepad and just running the file - you can save yourself from having to see lots of MsgBoxs by replacing the MsgBoxs with WScript.Echos and running the VBScipt file in cmd.exe with CScript (CScript C:\filepath\file.vbs) – Taylor Scott Jan 25 '18 at 4:30
• Oh and you can add syntax highlighting for VBScript (or any VB derivative) using the markdown notation <!-- language-all: lang-vb --> – Taylor Scott Jan 25 '18 at 4:32

Use an empty variable instead of ""

Uninitialized variables are set to a special value: empty. When empty is cast to a string it becomes "".

Contrived example (using z instead of ""):

s=InputBox(z)
For i=1To 5
For j=1To i
r=r&s
Next
MsgBox r
r=z
Next


For numeric types Empty is 0, and for booleans it is false. This can cut down on initialization code.

a=inputbox("Enter Starting Value")
b=a
while done=0
i=i+1
b=b*2
done=(b>10000 or i>10)
wend
msgbox "Your "&a&" rabbit(s) have turned into "&b&" rabbit(s) in "&i&" years."


# Use IIf instead of If...Then

IIf() is essentially a ternary operator. You can potentially save quite a few bytes.

For example:

var = "World"

If foo Then
MsgBox "Hello" & var
Else
MsgBox "Goodbye" & var
End If


Vs.

MsgBox IIf(foo, "Hello", "Goodbye") & "World"


Note: VBA only. VBScript doesn't support this one.

• Iff() isn't an intrinsic VBScript function, thus it's not supported. – omegastripes Dec 5 '15 at 11:15
• Well, I'll be damned. I can't believe I never knew knew that. You're right. I'm going to leave it here because the tip will be useful for vba folks. – RubberDuck Dec 5 '15 at 11:35
• Unfortunately Iff() isn't an intrinsic VBA function as well :D – omegastripes Dec 5 '15 at 11:42
• Yes it is. Oh. Lol. Damned typo. – RubberDuck Dec 5 '15 at 11:43

## Shorter IFThen: save 10 bytes

If you terminate an IF/THEN clause with a carriage return, you don't need to use an END IF.

Example:

For x=1 to 1
IF x=1 THEN MSGBOX "No Error"
Next


# Use Line Separators

Newlines count as two bytes ("\n"), but a colon is only one.

For i = 0 To 10
MsgBox "Hello"
Next


Vs.

For i = 0 To 10: MsgBox "Hello": Next


Note: You can't use the line separator for If statements.

• Newlines only count as one byte on Unix Systems, however, Windows Systems use \r\n (Which is two bytes, 13 and 10 respectively) – ATaco Mar 29 '17 at 1:20

# Condense For statements

In For statements (and in fact any statement comprised of a " or numeric literal followed by a reserved command) the space that separates any numeric and a following word may be removed

This means that

For i = 1 To 100 Step 2
...
Next


May be condensed down to

For i=1To 100Step 2
...
Next


and following this logic,

If t = "TEST" Then ...


may be condensed to

If t="Test"Then ...