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 .. 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, but there are some differences that will be described here.

The difference in commands separation

In the VBScript language, each command is located on an individual row (i.e. the comands are separated by rows). In the JavaScript language, each command must be separated by a semicolon and so it is possible to place multiple commands on a single row (although it is not wise to exaggerate this approach).
- for VBScript:
a = 1
b = 1 - Two commands of value assignment located on individual rows
- for JavaScript:
a = 1;
b = 2; - The two commands 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 command, whereas in the VBScript language, two commands 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 command must be used for assigning an object.
- for VBScript:
Dim nVal, oPanel - The Dim command 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 command 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 command 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 command. But in the JavaScript language, the syntax is different.
- for VBScript: The condition is executed by the If...Then...Else commands
If a > 1 Then
End If
- for JavaScript: The condition is executed by the if...else commands. IF there are multiple commands in the conditions then these are written in brackets {...}.
if (a > 1)

The cycle command FOR

Both languages has the FOR command that allows to go through a portion of code multiple times. In the JavaScript language, this command has a slitly more comlicated syntax, but it is much more general.
- for VBScript: The For...Next command
For i=0 To 100
- for JavaScript: The for command has three parameters.
- 1st parameter (e.g. i=0) is the command 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 command that is executed at the end of the cycle in order to modify the cycle variable

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

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

Command for manifold condition

Both languages has the command for manifold condition, when a set of commands is executed only if a condition reaches a defined status.
- for VBScript: The Select Case command
Case 1
   sMsg = "ok"
Case 2
   sMsg = "warning"
Case 3
   sMsg = "alarm"
End Select
- for JavaScript: The switch command. The break; is written after each command set that terminates the conditions and jumps off the switch command.
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