There are three types of operators that can be used in programs:
Calculation Operator
In the table below, the calculation result assumes variable A
is 3 (0000 0000 0000 0011), and the variable B
is 4 (0000 0000 0000 0100).
Remainder calculation and all Bitwise calculations will be performed with 32-bit integer dataOperators are special keywords or characters that either return a value or perform some action using one or more input expressions.
Unlike regular functions, operators are called without using parentheses. Operators are usually called in one of three ways:
Prefix:
<operator> <expression 1>
. Example:typeof A
Midfix:
<expression 1> <operator> <expression 2>
. Example:A + B
Postfix:
<expression> <operator>
. Example:A++
JavaScript operators include common mathematical functions like +
and *
, Boolean logic functions like &&
(AND) and ||
(OR), and other miscellaneous functions.
Note that JavaScript ES5 does not allow you to create your own operators, nor can you change the behavior of existing operators for specific objects.
Table of Contents |
---|
Mathematical Operators
Mathematical operators take two numbers as input and return a single number as output.
For these examples, assume A
is 3 and B
is 4.
Operator | Function | Example | Description |
| Bitwise invert |
| Each bit of A (0000 0000 0000 0011b) is inverted. The result is 1111 1111 1111 1100b.|
| Addition |
| Adds Variable | ||||
| Subtraction |
| -
| Subtracts Variable | |||
| Multiplication |
| Multiplies Variable | ||||
| Division |
| Divides Variable | with is divided by | |||
| Remainder |
| Indicates the remainder from the division of | ||||
| Bitwise AND |
| Bitwise logical AND calculation of A and B. The result is 0. | ||||
| Bitwise OR |
| Bitwise logical OR calculation of A and B. The result is 0111b (= 7). | ||||
| Bitwise XOR |
| Bitwise logical XOR calculation of A and B. The result is 0111b (= 7). | ||||
| Bitwise Shift Left |
| Shifts the bits of A to the left as much as B. The rightmost part is filled with 0. The result is 0011 0000b (= 48). | ||||
| Bitwise Shift Right |
| Shifts the bits of A to the right as much as B. The leftmost part is filled with 0. The result is 0. |
Logical/Comparative Operator
In the table shown below, the calculation result assumes that variable A
is 1 (True) and variable B
is 0 (False). In Canvas, the values other than 0 are all processed as True. Only 0 is recognized as False.
When the calculation result is true, the result is always 1.
In JavaScript ES5, all numbers are treated as 64-bit floating-point numbers. The BigInt type is not currently supported.
JavaScript ES5 does not have an operator for exponentiation. Instead, use the Math.pow function. For example, Math.pow(2, 3) returns 8 (2 raised to the power of 3).
Logical Operators
Logical Operators take in Boolean values and output another Boolean value.
Operator | Function | Example | Description | |
| Logical AND |
| If value in variables | If it is falseIn other cases, the result is 0. |
| Logical OR |
| If value in variables | 0. |
| Logical NOT |
| Returns the logical inverse. If A evaluates to |
Note: If the input expressions are not Boolean, they will be automatically coerced into a Boolean value. In JavaScript, there are only 6 values that evaluate to false
:
false
undefined
null
Nan
(Not a Number)0
""
(empty string)
All other values evaluate to true
.
Comparison Operators
Comparison operators compare two expressions and return true
if the comparison is true, and return false
otherwise.
Operator | Function | Example | Description |
| Less than |
| If |
less than |
>
| Greater than |
| If |
| Less than or equal to |
| If |
less than or equal to | |||
| Greater than or equal to |
| If |
| Equal to |
| If Note: the comparison operator |
| Instance of |
| If |
| Is not equal to |
| If |
...
| In |
| If the specified property |
Assignment Operators
Operator | Function | Example | Description | ||
| StoreAssignment |
| Stores the value in expression Returns the value of Note: the assignment operator Example:
| ||
| Addition Assignment |
| Stores the value | and uses it as a result.
...
. Equivalent to Returns the value of | |||
| Subtraction Assignment |
| Stores the value Returns the value of |
| Multiplication Assignment |
| Stores the value Returns the value of |
| Division Assignment |
| Stores the value Returns the value of |
| Increment Operator |
| Adds 1 to the value of the variable Returns the value of |
| Decrement Operator |
| Subtracts 1 from the value of the variable Returns the value of |
The assignment operator =
can be used consecutively, as shown below:
A = B = C;
In this case, the value of A
and B
will be set as to the value of C
. In other words, the value at on the rightmost side of the operator will be treated as used to set the result value of the “=” operationthe variables on the left. The example mentioned above will be processed internally in the program as shown below.:
B
←C
A
←B
Bitwise Operators
A bitwise operator acts on the bitwise representation of a variable.
All Bitwise operators are performed by converting the inputs into 32-bit signed integers. The 32-bit integer result will be converted back into a 64-bit floating-point number.
In the table below, the calculation result assumes that variable A
is 3 (0000 0000 0000 0011), and that variable B
is 4 (0000 0000 0000 0100).
The bitwise representation of a tag value may not reflect how the tag is stored on a PLC. In most cases, bitwise operations should be performed by a PLC.
Operator | Function | Example | Description |
| Bitwise invert |
| Each bit of |
| Remainder |
| Indicates the remainder from the division of |
| Bitwise AND |
| Bitwise logical AND calculation of |
| Bitwise OR |
| Bitwise logical OR calculation of |
| Bitwise XOR |
| Bitwise logical XOR calculation of |
| Bitwise Shift Left |
| Shifts the bits of |
| Bitwise Shift Right (Signed) |
| Shifts the bits of |
| Bitwise Shift Right (Zero Filling) |
| Shifts the bits of |
Special Operators
There are a few operators that do not fit clearly into the above categories.
Operator | Function | Example | Description | ||||
| Check Type |
| Returns a string indicating the type of the specified variable or expression. Possible return values include “string”, “number”, “boolean”, “object”, “function”, “null”, and “undefined”. | ||||
| Void |
| Evaluates the specified expression but always returns For example:
This will execute the function | ||||
| New Instance |
| Used to get a new instance of a user-defined object or one of the built-in object types like In the example below, we define a class
| ||||
| Delete Property |
| Deletes a property from an object. In the example below, we delete the
| ||||
| Comma Operator |
| The comma operator takes in two expressions, evaluates them in order, and returns the expresion on the right. In practice, this can be used to perform multiple actions at the end of a for loop. The example below contains a
| ||||
| Conditional Ternary |
| Checks whether the statement
This is equivalent to the script below.
|