VBA->JS: Variable declaration & semicolons (Syntax)

In both VBA and JavaScript, as with most programming languages, those things with which code works are held in “variables”. Both VBA and JavaScript declare variables and assign values to them.
Comparing how they do it:

Dim str
var str = "string value";
str = "string value"

…Three major differences are apparent:

  1. VBA uses the keyword “Dim” to indicate a variable is being declared.
    JavaScript uses the keyword “var”, which is much more intuitive!
  2. VBA requires a second line of code to assign a value.
    JavaScript allows you to assign a value in the same line of code as the declaration (but it’s not mandatory).
  3. JavaScript has a semicolon at the end of the expression.
    The semicolon is mandatory at the end of statements. Do not, however, take this to mean that you need a semicolon at the end of every line of code!

    • Unlike VBA, a JavaScript expression or statement may break across multiple lines of code – the semicolon indicates to the program executing the code where the end of the “line” is:
str = "The entire string must" str = "The entire string can
str = str & " must be on one" be broken across multiple
str = str & " line or the" lines - the semicolon at the
str = str & " assignment must" end indicates the end of
str = str & " be broken up." the assignment.";
    • There is no semicolon at the end of a function declaration or at the end of a comparison or looping expression such as if, for, foreach or while:
Function Abc() function Abc()
If x = 0 Then if(x===0)
For i = 1 to 10 for(i==1, i < 11, i++)

Now, the VBA purist is certainly objecting to the declaration Dim str and is asking, "Shouldn't it be Dim str As String? Yes, indeed, that would be more correct. But there is no equivalent in JavaScript! (And if you feel horrified I guarantee you it's nothing compared to what any C# developer feels on encountering this fact.) While JavaScript does recognize various data types, it's not possible to assign a data type to a variable.

Does this mean that variables in JavaScript are like the VBA Variant type? Not exactly, but it's similar enough for you to use that comparison to begin with. JavaScript does recognize a data type and use applicable rules for it. However, as with a variable of type Variant, the data type of a JavaScript variable is not set in stone: a value of another data type can be assigned to it freely without generating a "Type mismatch" error.

And this can, of course, be a source of maddening problems in your code - which is why we're encouraged to declare data types in VBA...

For more about data types, see [article to follow].

P.S. Almost forgot something very important! JavaScript will let you declare a variable without using a keyword, just like VBA if the Option "Require Variable Declaration" is not activated. Of course, VBA developers "in the know" know to make sure that's activated on a new installation of Office! Some JavaScript editors can check for variable declaration and give you a warning, but most of those you'll probably use to get started won't have that kind of option... So be aware and DON'T DO THAT :-)!

Leave a Reply