There are three types of operators that can be used in programs: Operators 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 work on take two numbers as input and return a single number as output.
In For these examples, assume A
is 3 and B
is 4.
Operator | Function | Example | Description |
| Addition |
| Variable |
| Subtraction |
| Variable |
| Multiplication |
| Variable |
| Division |
| Variable |
| Remainder |
| Indicates the remainder from the division of |
...
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 |
| Logical OR |
| If value in variables |
| Logical NOT |
| Returns the logical inverse. If A evaluates to |
Note: If the input expressions are not Boolean, they will be converted automatically coerced into a Boolean value. In JavaScript, there are only 6 values that are evaluated as evaluate to false
:
false
undefined
null
Nan
(Not a Number)0
""
(empty string)
All other values are evaluated as 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 | ||
| Greater than |
| If | ||
| Less than or equal to |
| If | ||
| Greater than or equal to |
| If | ||
| Equal to |
| If Note: the comparison operator | ||
| Instance of |
| If | ||
| Is not equal to |
| If | ||
| In |
| If | there is an instance of the specified property |
|
Assignment Operators
Operator | Function | Example | Description | ||
| Assignment |
| Stores the value in variable expression Returns the value of Note: the assignment operator Example:
| ||
| Addition | assignmentAssignment |
| Stores the value Returns the value of | |
| Subtraction assignmentAssignment |
| 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:
...
In this case, the value of A
and B
will be set to the value of C
. In other words, the value on the rightmost side of the operator will be used to set the value of the 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 bit bitwise representation of a variable.
All Bitwise operators are performed by converting the inputs into 32-bit signed integerintegers. 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 | uses always returns | as the value. This can be used for anonymous function calls, or more generally when an expression is used to perform some action without the need for a return value. For example:
This will execute the function | Thus, if If you try to use | |||
| 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 pracicepractice, this can be used to perform multiple actions at the end of a for loop. The example below contains a
| ||||||
| Conditional Ternary | Comparison
| Checks whether the statement | where each case has a value associated with it.
This is equivalent to the script below.
|