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

In this last post on conditionals and loops I’ll present Arrays and how to loop using the last kind not yet discussed: “For Each”.

Unlike the other kinds of loops, “For Each” doesn’t use a counter or any kind of arbitrary way to force a loop. So, in order to discuss “For Each” loops it helps to have something to loop through.

In VBA, “For Each” works with collections of objects. It can also be used with an array of data type Variant, but I’ve not encountered that very often. Mostly, you’ll have worked with collections belonging to an Office object model.
In JavaScript, you’ll mostly loop through arrays and the elements of objects – things your code has generated.

While the logic of arrays is the same in both VBA and JavaScript, the syntax differs (nothing new about that).

Declare and populate arrays
Dim str(2) as String
str(0) = "one"
str(1) = "two"
str(1) = "replace two"

Dim i() as Long
Redim Preserve i(2)
i(0) = 1
i(1) = 2

Dim a as Variant
a=Array(1, "a", 5.7)
a(2) = 6.1

var a2 = [];
a2[0] = "one";
a2[1] = "two";
a2[3] = "four";

var a1 = [1, "a", 5.7];
a1[2] = 6.1;

Important things to note:

  1. As with the default in VBA, arrays are zero-based; there is no option to change that to one-based.
  2. In contrast to VBA, JavaScript has only the one syntax for declaring an array.
  3. JavaScript does not provide a way to enforce an array’s data type.
  4. A list of elements is assigned using a pair of square brackets.
  5. JavaScript has no fixed length arrays, only dynamic.
  6. The number of array elements can be changed by simply specifying an index.
  7. Array elements can be omitted. (In that case, when no value has been assigned to an index the value “Undefined” is returned.)
  8. Elements of an array can be removed using the delete method: delete a2[1];. (In this case, again, “Undefined” will be returned for the deleted element’s index.)
  9. The data type of an array is Object.

For Each / for-in loops
And now for the last type of loop. JavaScript refers to it as a “for-in” loop, the word “Each” does not appear:

Dim v as Variant, s as String
For Each v in a
s = s & v & Chr(13)
Debug.Print s
var s = "";
for (var i in a2) {
s += a2[i] + '\n';

As you can see, it follows the same pattern as other conditionals with the comparison in parentheses, followed by the opening brace.
Analog to a “for” loop, the counter variable can be declared, as here, in the condition or before it.

What’s new
The expression in the loop contains two new syntax elements:

  1. You can shorten things when adding to a string in a loop (or elesewhere) by using the assignment operator: +=. This is the same as typing: s = s +, something you won’t often see in JavaScript.
  2. In VBA the function Chr() is often used to specify special control characters. In JavaScript, a set of “escape sequences” is used for the most common symbols. These can be included directly in a literal string: "a new line at the end of the string\n" and must not be concatenated on as you need to do in VBA: a new line added to a string" & Chr(13).
    \n, used here, is for a new line. You’ll find a list of escape sequences here.

    The next topic will be functions – getting into the nitty-gritty…

Leave a Reply