There would be no complaints if you called this function with 0, 1 or 2 arguments, but how to detect what was passed?

Let's assume that arg1 was an object.

if(!arg1) { ... }

would be good enough to test if a had been passed with a valid object address, but if arg1 was, say a boolean, and had the value false, then !a would yield the same result as if arg1 was not present. So we need another way.

Since javaScript will convert to a string to compare to another string, comparing a missing value against a string will cause it to be converted to 'undefined'. We can leverage this as below to detect a missing argument.

if (arg1 == 'undefined' ) {... arg was missing ...}

However, this would fall apart if 'undefined' was a valid value for arg1.

typeof Operator to test for optional/missing argument

Luckily we can use the typeof operator instead.

if (typeof arg1 == 'undefined') {... arg was missing ...}

Since we are going to be using this a lot, let's make a convenient function of it

function isUndefined ( arg) {

return typeof arg == 'undefined';

}

So in future we can conveniently say,

if (isUndefined(arg1)) {...arg1 was missing .. }

And to minimize conversion, do a vbaHack of

function IsMissing(arg1) {

return isUndefined (arg1);

}

Dealing with default value for optional argument

We are going to convert an existing VBA project, though, so it's still going to be a pain to deal with each optional argument, especially if it has an optional parameter. Here's a generalized function we can use to deal with missing arguments and their default value