VBA->JS: JavaScript data types (Concept)

Data types determine what can be done with the content in a variable. For example, Numbers can be manipulated arithmetically; Boolean is used to test a variable’s state: does it or does it not meet a specified criterium.

Two of the data types used in VBA are also found in JavaScript and are used in the same way: String and Boolean.

JavaScript also supports the numerical data type, but in contrast to VBA and most other programming languages there’s only the one: Number. It’s not broken down into various integer and decimal types that exist primarily to limit memory storage requirements. So no need to worry about whether the value you’re assigning to a variable will be Integer or Long, Single or Double! JavaScript just goes whole hog and gives you 64-bit double precision floating point…

Then there are three data types not used in VBA:Null, Undefined and Object. Some aspects of these will be a familiar, but on the whole these are new for those steeped in VBA and have to be learned. Here’s a short explanation:
Null: A variable is of data type Null when it has been set explicitly to the value null. In VBA the value Nothing is used where JavaScript (and C#) use null. It is used with objects (as opposed to numbers, strings or boolean values). One big difference with null in JavaScript is that an object that has had no content yet assigned to it will not be null (or Nothing); instead, it will evaluate to Undefined
Undefined: The value undefined is returned when no content has been assigned to a variable. This is different from VBA, where an unassigned object variable returns Nothing, a number returns 0 (zero) and a string returns an empty string (”).
Object: Now we’re getting into complex territory and you need to put on your thinking cap. Objects in JavaScript aren’t simply a data type: Objects are the very basis of JavaScript.

When working in VBA the Office application provides objects that represent the host Application, its file (whether a Workbook, a Slide Show, or a Document), methods for working with the application and file, and properties that describe them, many of which themselves return an object with its own methods and properties. We’re all familiar with and work with these rich APIs. For advanced projects developers can create their own objects, methods and properties, using classes, but this is probably more the exception than the rule for those programming with VBA.

This makes sense for VBA because it runs inside a host application and is mainly used to extend that application and its content. The situation is somewhat different for programming languages such as C# or VB.Net that are used to create entire applications. Such applications are built around developer-defined classes and their objects, properties and methods.

JavaScript works more like VBA in that it runs in a host environment (usually a web browser) and can (and usually does) work with the web page hosted by the browser. In contrast to VBA, however, JavaScript cannot primarily control the browser or the environment in which the browser is running (security considerations). Its focus is mainly on the document (web page) and functionality running behind the scenes. For example, it can communicate with a database or a web service.

(So how do Web Add-ins fit into this? Web Add-ins run in an html document inside a browser window control, hosted in a custom task pane inside the Office application <whew>. The Web Add-in’s JavaScript can control both the html document and, as far as the Office APIs permit, the Office document hosted by the Office application.)

For such tasks, the developer uses Objects. But unlike Objects in other programming languages, such as VB.NET or C# where Object instances are created from classes, JavaScript Objects “just are” – JavaScript does not use the concept of classes. Objects are very powerful, but a bit “slippery” and take careful management. If you wish to delve more deeply into the topic, you’ll want to research it in JavaScript documentation and tutorials.

Since variables can’t be strongly typed it can happen that at any particular moment a variable could contain information of a different data type than was held previously. So you may want to check what data type is a variable’s content is. For this, JavaScript provides the typeof method:

var n = 3;
typeof n; //returns number
n = "alpha";
typeof n; //returns string

JavaScript, like VBA, provides some useful, basic functionality in the form of properties and methods for each of these datatypes, which can be called on a variable containing a value of the data type. This will be the topic of my next post.

Leave a Reply