VBA->JS: Conditionals, Loops & Operators 2 (Syntax)

Now that you’ve familiarized yourself with the logical operators JavaScript uses it’s time to look at the basic conditional constructs. Looping constructs are the topic of the next post.

Conditional If
Logically, If works the same way in JavaScript as in VBA, but the syntax and the operators used are different.

VBA JS
Dim i as Long
Dim result as String
i = 3
If i = 3 Then
result = "Do things"
Else If i <> 5 Then
result = "Do something else"
Else
result = "Last resort"
End If
var i = 3;
var result = '';
if ( i === 3) {
result = "Do something";
} else if( i !== 5) {
result = "Do something else";
} else {
result = "Last resort";
}

Notice the placement of the curly braces. Theoretically, the curly braces could be just about anywhere as JavaScript generally ignores white space (including new lines). (They can even be left out if what is to be executed when the condition is met is only one line, but that can be confusing. And if you later add more lines to be executed if the condition is met and forget the curly braces, you’re in trouble!) But the JavaScript compiler does have a tendancy to insert semicolons where it thinks they may have been left out and there is one known situation where this is a problem. How to place the curly braces is, indeed, a subject that can lead to hot discussion

My take on the matter is that it’s easier, in the long run, to be consistent: If you develop good habits you’re less likely to make an avoidable mistake; and your eyes become accustomed to reading code blocks formatted the same way.

Also important (and putting the curly brace on the same line helps to get it right…) is to NOT put semicolons after the comparison or after the closing parentheses. But DO remember to use them at the end of the statements to be executed in the conditional block!

Note: Occasionally you’ll come across code that looks like:
var i = 3;
var result = '';
result = ( i === 3) ? result = "Do something" :
( (i===5) ? result = "Do something else" :
result = "last resort" );

This is exactly the same as the if code blocks. When the condition being tested is not too complicated and you know the syntax, then this form is understandable. On the one hand, it takes up less vertical space, on the other, I find it takes longer for me to “parse” the meaning – perhaps because I’m so accustomed to the block style. (This example, with three conditions, by the way, is very borderline.)

So many things to remember… But at least the same considerations come into play for the the other comparisons.

Switch
In VBA we use Select Case as an alternative to If when the number of comparisons exceeds two or three:

VBA JS
Dim i as Long
Dim result as String
I = 3
Select Case i
Case 3
result = "Do things"
Case <> 5
result = "Do something else"
Case Else
result = "Last resort"
End Select
var i = 3;
var result = '';
switch ( i ) {
case 3:
result = "Do something";
break;
case 5:
result = "Last resort";
break;
default:
result = ""Do something else"";
break;
}

Personally, I find Select/switch easier to follow and write than a long list of “Else If…Then” lines. But there are number of things to note about working with switch:

  1. You can perform only simple comparisons; you can’t check for anything but equality. Compare the JavaScript second “case” with that of VBA and also with the “else if” example. For complex comparisons you have to stick with if.
  2. The curly braces contain the entire switch block – there are no curly braces around the individual case conditions.
  3. A colon must follow the value being tested for a case.
  4. A break; statement ends each case condition. This is not mandatory, but if it’s not there execution “falls through” to the next case condition rather than exiting the switch block – which can lead to unexpected and incorrect results!


One Response to “VBA->JS: Conditionals, Loops & Operators 2 (Syntax)”

  1.   Felipe Costa Gualberto Says:

    The return curly braces issue is interesting! Didn’t know about that.

Leave a Reply