Comparison of the VBScript and JavaScript languages

The PROMOTIC system uses the VBScript language for scripting since the first version back in 1995. The time goes on and it is now clear that this language is no longer very modern. It is supported by Microsoft software products, but not by other manufacturers (e.g. Internet Explorer supports it but Firefox, Chrome, Opera, Edge .. do not). During the time, the JavaScript language reached a very strong position and is considered to be the esssence of the current internet technologies.
The PROMOTIC system is also gradually adapting to this trend. When designing a PROMOTIC application, the designer may encounter the JavaScript syntax on these locations:
- in the PmPanel object in the Scripting language settings configurator.
The purpose of this chapter is to show that although the JavaScript language is considered to be more general (and therefore also more complex), for the PROMOTIC system purposes the written code seems simpler and in many cases easier to understand than the code written in VBScript language. The syntax is often similar to the VBScript language. There are some differences that will be described here.

The difference in writing capital letters

Both languages differ from the point of view of using capital letters in names of variables, properties and methods.

The JavaScript language is case sensitive. Therefore there is a difference e.g. between the name Value and value. The names of properties and methods must be written exactly as defined in the documentation.

The VBScript language is NOT case sensitive. The property named Value can be written as value, VALUE etc. But it is also recommended to write the system names as defined in the documentation.


Default properties or methods

The VBScript language supports the concept of "default properties" (or methods). Each object can have one property or method defined as default and therefore it is not necessary to write it in the script.


The PmData object has Item method defined as default method. The same way a variable in PmData (object Var) has a default property Value. Therefore it is possible to use multiple ways to access the variable value "Temperature1":

oData.Item("Temperature1").Value = 95
oData("Temperature1").Value = 95
oData.Item("Temperature1") = 95
oData("Temperature1") = 95
The JavaScript language does not support this approach and so it is necessary to write each property or method. I.e. it must be written:
oData.Item("Temperature1").Value = 95;

The difference in statements separation

In the VBScript language, each statement is located on an individual row (i.e. the comands are separated by rows). In the JavaScript language, each statement must be separated by a semicolon and so it is possible to place multiple statements on a single row (although it is not wise to exaggerate this approach).
- for VBScript:
a = 1
b = 1 - Two statements of value assignment located on individual rows
- for JavaScript:
a = 1;
b = 2; - The two statements can be placed to individual rows
a = 1; b = 2; - but both can also be on a single row

The difference in calling methods

For calling methods, the JavaScript language seems to be easier to use, because there are always brackets written after the method name (no regard if the method returns a value or not).
- for VBScript:
Pm.Debug "some text" - there are no brackets here, because there is no value to be returned
PmCommMsg.Run - again, there are no brackets here and the method hasno parameters
data = Pm.FileTextRead("#data:file.txt", 1) - there are brackets here, because the returned value is important for us
- for JavaScript:
Pm.Debug("some text"); - the brackets are used always
PmCommMsg.Run(); - the brackets are written even though the method has no parameters
data = Pm.FileTextRead("#data:file.txt", 1); - the brackets are written again, in this case the syntax is identical with the VBScript

Variable creation and initialization

In the JavaScript language, it is easier to create a variable and assign a value there. It can be done by a single statement, whereas in the VBScript language, two statements are needed. For the assignment, there is no diffrence whether a simple value or an object is assigned, whereas in the VBScript language, the Set statement must be used for assigning an object.
- for VBScript:
Dim nVal, oPanel - The Dim statement creates the variable, but no value can be assigned
nVal = 1
Set oPanel = pMe.Pm("/Panel1")
- for JavaScript:
var nVal = 1, oPanel = pMe.Pm("/Panel1"); - The var statement creates variables and can also assign values

Language operators

Both languages share many common operators, for example addition (+), subtraction (-), multiplication (*), division (/) etc. But the JavaScript language has more operator that may come handy. For example, the bit shift (<< and >>), conditional expression (a ? b : c), summation/subtraction of one (++ and --).

Especially practical is the simplification of value adding (e.g. to the variable in the PmData object):

- for VBScript: Adding the value of 15 to the variable means repeating also the right portion of the statement expression.
pMe.Pm("/Data/#vars/Temperature").Value = pMe.Pm("/Data/#vars/Temperature").Value + 15
- for JavaScript: Adding the value of 15 to the variable is much simpler thanks to the += expression, which does not mean "assign", but "add". There are similar expressions for "subtract", "multiply", "divide" ..
pMe.Pm("/Data/#vars/Temperature").Value += 15;
See also The JavaScript operators and syntax description.

Language functions

Both languages have a large set of functions (in the VBScript language for example the functions Now, Abs ..). The JavaScript language also has a great number of similar functions, but it is not recommended to use them - it is better to use just the Pm object methods.

Language data types

Both languages has the data types like "integer", "Real number", "String", "Object". The main difference is that in the JavaScript language has different types for "Date" and "Array". Both these types are used in the PROMOTIC system very often, but the implementation style of these types in the JavaScript language is not suitable for the purposes of the PROMOTIC system. The problem has been solved as follows:
- for the "Date" type: In the VBScript language, the date is "something like a real number" that is not clear after a brief examination, what year, month, day, hour, minute, second, millisecond. it means. For this purpose, there are auxiliary VBScript functions, that can be used for translating this "real number" into an apparent year, month, etc.

Similar concept is used also for the JavaScript language: All PROMOTIC methods, that return a date, return "something like a real number". This value can then be transmitted to other methods and most importantly, the new PmDateObject object can be used for decoding the year, month, day ..

- for the "Array" type: The VBScript language has the "Array" type (it can be created for example by the Array function). The most commonly used are the 1- and 2-dimensional variants.

Similar concept is used also for the JavaScript language: The new Pm.CreatePmArray method can be used for creating "Array" as a special PmArray object, and all PROMOTIC methods that should return "Array", return this object.


The IF condition

Both languages can create a condition by the IF statement. But in the JavaScript language, the syntax is different.
- for VBScript: The condition is executed by the If...Then...Else statements
If a > 1 Then
End If
- for JavaScript: The condition is executed by the if...else statements. IF there are multiple statements in the conditions then these are written in brackets {...}.
if (a > 1)

The cycle statement FOR

Both languages has the FOR statement that allows to go through a portion of code multiple times. In the JavaScript language, this statement has a slitly more comlicated syntax, but it is much more general.
- for VBScript: The For...Next statement
For i=0 To 100
- for JavaScript: The for statement has three parameters.
- 1st parameter (e.g. i=0) is the statement that is executed at the beginning of the cycle.
- 2nd parameter (e.g. i<100) is an expression that is tested at the end of the cycle in order to determine whether to continue or not
- 3rd parameter (e.g. i++) is the statement that is executed at the end of the cycle in order to modify the cycle variable

If there are multiple statements to be repeated in the for statement then these are written in the brackets {...}.

for (i=0; i<=100; i++)

Statement for manifold condition

Both languages has the statement for manifold condition, when a set of statements is executed only if a condition reaches a defined status.
- for VBScript: The Select Case statement
Case 1
   sMsg = "ok"
Case 2
   sMsg = "warning"
Case 3
   sMsg = "alarm"
End Select
- for JavaScript: The switch statement. The break; is written after each statement set that terminates the conditions and jumps off the switch statement.
switch (nVar)
case 1:
   sMsg = "ok"; break;
case 2:
   sMsg = "warning"; break;
case 3:
   sMsg = "alarm"; break;
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice