VBA->JS: Case sensitivity (Syntax)

When working in VBA it doesn’t matter, for the most part, whether you type capital or small letters. VBA will monitor and compare with its internal list of keywords as well as variables you’ve declared and make corrections as you go. If there are variables that aren’t declared there are no automatic corrections, but VBA doesn’t care. On the whole, you only have to be careful when typing Style names and Find expressions when “Match Case” is in-force.

JavaScript is very different in this respect: Which case you use matters. If even one character in a term is cased differently, it’s considered a distinct term. For example, declaring variables:

Dim Abc as String
Dim abc as String
'VBA changes both variable names to
'match and throws the compiler error:
'Duplicate declaration in current scope."
var Abc = "Abc";
var abc = "abc";
//Both are accepted
//and are two distinct

For another example, suppose you’re stepping through code and checking values in the VBA editor’s Immediate Window, as below. If you don’t pay attention to case, VBA doesn’t care. JavaScript, on the other hand, returns an error if you work in an editor that checks such things. Otherwise, your code simply fails…

Dim abc as String
abc = 10
var abc = "abc";
'In the Immediate Window
var Abc = "Abc";
//ReferenceError: aBc is not defined

This definitely takes some getting used to, especially for those of us with lazy fingers!

At this point, you’re probably wishing you knew where and how you can try these things out for yourself. In my next post, I’ll point out a useful tool I came across while reading up on JavaScript for testing these things.

Leave a Reply