The JavaScript operators and syntax description
The
JavaScript language expression is similar to
VBScript language expression, but has more operators and therefore is more general and versatile (see
Operators list of the VBScript language). The PROMOTIC system therefore prefers to use the
JavaScript language expression.
Comparison:
- The expression addition, subtraction, multiplication, division ... is identical in
JavaScript and
VBScript language, e.g.
(4 * x + 1) / 3
- The expression for logical operations like "and", "or" ... are different, e.g.
- Very handy conditional
JavaScript expression (e.g.
b ? 1 : 2) cannot be written in
VBScript.
The
JavaScript expression is used in the PROMOTIC system:
- in the
PmExpr object that is used for dynamic evaluation of
JavaScript expression.
List of
JavaScript expression operators:
Arithmetic operators:
operator |
Description |
v1 + v2 |
Addition of numbers or strings addition |
v1 - v2 |
Subtraction of numbers |
v1 * v2 |
Multiplication of numbers |
v1 / v2 |
Division of numbers |
v1 % v2 |
Modulo (remainder after division v1/v2) |
v1 ? v2 : v3 |
If the v1 value is true, then the v2 value is returned, otherwise the v3 value is returned |
v1++ |
Increment of a number = increments a variable by one (v1 = v1 + 1) |
v1-- |
Decrement of a number = decrements a variable by one (v1 = v1 - 1) |
v1 += v2; |
Shortened notation for addition (v1 = v1 + v2) |
v1 -= v2; |
Shortened notation for subtraction (v1 = v1 - v2) |
v1 *= v2; |
Shortened notation for multiplication (v1 = v1 * v2) |
v1 /= v2; |
Shortened notation for division (v1 = v1 / v2) |
Example:
JavaScriptSelect and copy to clipboard
var val;
val = 1 + 2;
// Returns 3
val = true + 1;
// Returns 2
val = false + false;
// Returns 0
val = 6 + "Boiler";
// Returns "6Boiler"
val = "Boiler" + false;
// Returns "Boilerfalse"
val = "Boiler" + "Valve";
// Returns "BoilerValve"
val = 12 % 5;
// Returns 2
val = -1 % 2;
// Returns -1
val = 1 % -2;
// Returns 1
val = -4 % 2;
// Returns -0
val = 5.5 % 2;
// Returns 1.5
val += 3.5;
val -= 6;
Comparison operators:
operator |
Description |
v1 == v2 |
Returns true if v1 and v2 values are the same |
v1 != v2 |
Returns true if v1 and v2 values are not the same |
v1 >= v2 |
Returns true if v1 is greater or equal v2 |
v1 > v2 |
Returns true if v1 is greater than v2 |
v1 <= v2 |
Returns true if v1 is less then or equal v2 |
v1 < v2 |
Returns true if v1 is less than v2 |
- The PROMOTIC system so far does not support strict JavaScript comparison operators of the === and !== type.
- Two values of the
String type (text) are compared based on standard lexicographical ordering, using
Unicode values.
- When comparing a number and a text (String), the text is converted to a number value.
- If one of the operands is of the Boolean type, then this operand is converted to 1 if it is true and 0 if it is false.
Example:
JavaScriptSelect and copy to clipboard
var val;
val = 1 == 1;
// Returns true
val = 1 == "1";
// Returns true
val = "1" == 1;
// Returns true
val = 0 == false;
// Returns true
val = 1 != 2;
// Returns true
val = 1 != "1";
// Returns false
val = 1 != true;
// Returns false
val = 0 != false;
// Returns false
Logical operators:
v1 && v2 |
Logical AND (returns true if v1 and v2 is true) |
v1 || v2 |
Logical OR (returns true if v1 or v2 is true) |
!v1 |
Logical NOT (returns true if v1 is false) |
Bit operators:
operator |
Description |
v1 & v2 |
Bitwise AND (each bit of return value is a result of AND operation of corresponding bits in v1 and v2) |
v1 | v2 |
Bitwise OR (each bit of return value is a result of OR operation of corresponding bits in v1 and v2) |
v1 ^ v2 |
Bitwise XOR (each bit of return value is a result of XOR operation of corresponding bits in v1 and v2 |
~v1 |
Bitwise NOT (each bit of return value is the result of NOT operation of corresponding bit in v1). Not implemented in the PROMOTIC system so far. |
v1 << v2 |
Bitwise leftshift (the bit values in v1 are shifted by v2 number of bits to the left). For example v1<<2 returns double the v1 value. |
v1 >> v2 |
Bitwise rightshift (the bit values in v1 are shifted by v2 number of bits right). For example v1>>2 returns the whole number division of v1/2. |
The bit operators in the
JavaScript language require whole number values (4-byte integer).
If the value is not a integer, then the operator has to transform it to integer first.
If the value cannot be transformed, then the operator returns the
NaN value (it can be tested by the
Pm.IsValid method).