---
title: "13.1.4 Operators"
canonical: "https://manual.cimon.com/space/CCANVAS/4337303553/13.1.4%20Operators"
format: markdown
---
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:

1. Prefix: `<operator> <expression 1>`. Example: `typeof A`
2. Midfix: `<expression 1> <operator> <expression 2>`. Example: `A + B`
3. 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.

> Macro (toc)

# <span style="color: #0747a6">**Mathematical Operators**</span>

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** |
| `+` | Addition | `A + B` | Variable `A` and `B` are added. The result is 7. |
| `-` | Subtraction | `A - B` | Variable `B` is subtracted from `A`. The result is -1. |
| `*` | Multiplication | `A * B` | Variable `A` and `B` are multiplied. The result is 12. |
| `/` | Division | `A / B` | Variable `A` is divided by `B`. The result is 0.75. |
| `%` | Remainder | `A % B` | Indicates the remainder from the division of `A` with `B`. The result is 3. |

> 📝 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).

# <span style="color: #0747a6">**Logical Operators**</span>

Logical Operators take in Boolean values and output another Boolean value.

|  |  |  |  |
| --- | --- | --- | --- |
| **Operator** | **Function** | **Example** | **Description** |
| `&&` | Logical AND | `A && B` | If value in variables `A` and `B` are true, the result is 1. In other cases, the result is 0. |
| `||` | Logical OR | `A || B` | If value in variables `A` or `B` are true, the result is 1. In other cases, the result is 0. |
| `!` | Logical NOT | `!A` | Returns the logical inverse. If A evaluates to `true`, the result is `false`. If A evaluates to `false`, the result is `true`. |

**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`.

# <span style="color: #0747a6">**Comparison Operators**</span>

Comparison operators compare two expressions and return `true` if the comparison is true, and return `false` otherwise.

|  |  |  |  |
| --- | --- | --- | --- |
| **Operator** | **Function** | **Example** | **Description** |
| `<` | Less than | `A < B` | If `A` is less than `B`, the result is 1. In other cases, the result is 0. |
| `>` | Greater than | `A > B` | If `A` is greater than `B`, the result is 1. In other cases, the result is 0. |
| `<=` or `=<` | Less than or equal to | `A <= B` | If `A` is less than or equal to `B`, the result is 1. In other cases, the result is 0. |
| `>=` or `=>` | Greater than or equal to | `A >= B` | If `A` is greater than or equal to `B`, the result is 1. In other cases, the result is 0. |
| `==` | Equal to | `A == B` | If `A` is equal to `B`, the result is 1. In other cases, the result is 0. Note that types are not checked, so for example `1 == "1"` evaluates to `true`.<br>**Note**: the comparison operator `==` will not assign a value, unlike the assignment operator `=`. |
| `===` | Instance of | `A === B` | If `A` is equal to `B`, and both expressions have the same type, the result is 1. Otherwise, the result is 0. `1 === "1"` evaluates to `false`. |
| `!=` | Is not equal to | `A != B` | If `A` is not equal to `B`, the result is 1. In other cases, the result is 0. |
| `in` | In | `A in B` | If the specified property `A` is in `B`, returns `true`, otherwise `false`. Example:`"a" in {"a": 1, "b": 2}` returns `true`. |

# <span style="color: #0747a6">**Assignment Operators**</span>

|  |  |  |  |
| --- | --- | --- | --- |
| **Operator** | **Function** | **Example** | **Description** |
| `=` | Assignment | `A = B` | Stores the value in expression `B` to variable `A`.<br>Returns the value of `B`.<br>**Note**: the assignment operator `=` cannot be used to compare two values. Instead, use the comparison operator `==`.<br>Example:<br>```
var A = 1; // Sets A to 1
A == 2;    // Returns false
A = 2;     // Sets A to 2 and returns 2
A == 2;    // Returns true
``` |
| `+=` | Addition Assignment | `A += 10` | Stores the value `A + 10` to variable `A`. Equivalent to `A = A + 10`.<br>Returns the value of `A` *after* incrementing. |
| `-=` | Subtraction Assignment | `A -= 10` | Stores the value `A - 10` to A. Equivalent to `A = A - 10`.<br>Returns the value of `A` *after* decrementing. |
| `*=` | Multiplication Assignment | `A *= 10` | Stores the value `A * 10` to A. Equivalent to `A = A * 10`.<br>Returns the value of `A` *after* multiplying. |
| `/=` | Division Assignment | `A /= 10` | Stores the value `A / 10` to A. Equivalent to `A = A / 10`.<br>Returns the value of `A` *after* dividing. |
| `++` | Increment Operator | `A++` | Adds 1 to the value of the variable `A` and stores the value to `A`. Equivalent to `A = A + 1`.<br>Returns the value of `A` *before* incrementing. |
| `--` | Decrement Operator | `A--` | Subtracts 1 from the value of the variable `A` and stores the value to `A`. Equivalent to `A = A - 1`.<br>Returns the value of `A` *before* decrementing. |

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 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`

# <span style="color: #0747a6">**Bitwise Operators**</span>

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 | `~ A` | Each bit of `A` (0000 0000 0000 0011) is inverted. The result is (1111 1111 1111 1100). |
| `%` | Remainder | `A % B` | Indicates the remainder from the division of `A` with `B`. The result is 3. |
| `&` | Bitwise AND | `A & B` | Bitwise logical AND calculation of `A` and `B`. The result is 0. |
| `|` | Bitwise OR | `A | B` | Bitwise logical OR calculation of `A` and `B`. The result is 0111 (7). |
| `^` | Bitwise XOR | `A ^ B` | Bitwise logical XOR calculation of `A` and `B`. The result is 0111 (7). |
| `<<` | Bitwise Shift Left | `A << B` | Shifts the bits of `A` to the left by `B` bits. Fills using 0. |
| `>>` | Bitwise Shift Right (Signed) | `A >> B` | Shifts the bits of `A` to the right by `B` bits. Fills using a copy of the leftmost bit. |
| `>>>` | Bitwise Shift Right (Zero Filling) | `A >>> B` | Shifts the bits of `A` to the by `B` bits. Fills using 0. |

# <span style="color: #0747a6">**Special Operators**</span>

There are a few operators that do not fit clearly into the above categories.

|  |  |  |  |
| --- | --- | --- | --- |
| **Operator** | **Function** | **Example** | **Description** |
| `typeof` | Check Type | `typeof A` | 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` | Void | `void A` | Evaluates the specified expression but always returns `undefined`. 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.<br>For example:<br>```
void function foo() {
	notification.send("in foo");
}();
```<br>This will execute the function `foo` without adding `foo` to the namespace. If you try to use `foo` after line 3, an error will be thrown. |
| `new` | New Instance | `new Date()` | Used to get a new instance of a user-defined object or one of the built-in object types like `Date` or `Array`. In JavaScript ES5, this can be used to create user-defined classes without using the `class` keyword.<br>In the example below, we define a class `Student` using a constructor function. Then we create an instance of the class and store it to the variable `jim`.<br>```
function Student(name, id, age) {
  this.name = name;
  this.id = id;
  this.age = age;
}

var jim = new Student("Jim", 123456, 18);

notification.send(JSON.stringify(jim));
```<br>`new` is often used with the built-in `Date` type to get the current day, year, etc.<br>```
var oldTime = new Date("Dec 31 1999");
oldTime.getFullYear(); // 1999
var currentTime = new Date();
currentTime.getFullYear(); // returns the current year
``` |
| `delete` | Delete Property | `delete object.property` | Deletes a property from an object.<br>In the example below, we delete the `firstName` property from the object `employee`.<br>```
var employee = {
  firstName: 'John',
  lastName: 'Doe'
};

notification.send(JSON.stringify(employee));
// {"firstName":"John","lastName":"Doe"}

delete employee.firstName;

notification.send(JSON.stringify(employee));
// {"lastName":"Doe"}
``` |
| `,` | Comma Operator | `(a, b)` | 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.<br>The example below contains a `for` loop with two variables `i` and `j`. `i` *increases* by 1 each loop, while `j` *decreases* by 1.<br>```
var s = "";
for (var i = 1, j = -1; i <= 3; i++, j--) {
	s += "i: " + String(i) + ", ";
	s += "j: " + String(j);
	s += "\n";
}
notification.send(s);
// i: 1, j: -1
// i: 2, j: -2
// i: 3, j: -3
``` |
| `?`…`:` | Conditional Ternary | `a ? b : c` | Checks whether the statement `a` is `true` or `false`. If `true`, returns expression `b`. If `false`, returns expression `c`. This is essentially a more compact way of writing an `if`…`else` statement.<br>```
var isOn = tag.read("motorOn");
var s = isOn ? "Motor is on." : "Motor is off.";
notification.send(s);
```<br>This is equivalent to the script below.<br>```
var isOn = tag.read("motorOn");
if (isOn) {
    var s = "Motor is on.";
} else {
    var s = "Motor is off.";
}
notification.send(s);
``` |