There are three types of operators that can be used in programs:
Mathematical Operators
Mathematical operators work on two numbers and return a number.
...
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.
...
All other values are evaluated as true
.
Comparison Operators
Comparison operators compare two expressions and return true
if the comparison is true, and 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 smaller 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 . |
===
| 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 there is an instance of A in B , returns true , otherwise false . Example: 7 in [1, 3, 5, 7] returns true . |
Assignment Operators
Operator | Function | Example | Description |
=
| Assignment | A = B
| Stores the value in variable B to variable A . |
+=
| Addition assignment | A += 10
| Stores the value A + 10 to A. Equivalent to A = A + 10 . |
-=
| Subtraction assignment | A -= 10
| Stores the value A - 10 to A. Equivalent to A = A - 10 . |
++
| Increment Operator | A++
| Adds 1 to the value of A and stores the value to A . Equivalent to A = A + 1 |
--
| Decrement Operator | A--
| Subtracts 1 from the value of A and stores the value to A . Equivalent to A = A - 1 |
...
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 representation of a variable.
...
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. |
Special Operators
The following operators do not fit neatly 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 uses undefined as the value. This can be used for anonymous function calls. For example: Code Block |
---|
void function foo() {
notification.send("in foo");
}(); |
This will execute the function foo without adding foo to the namespace. Thus, 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. In the example below, we define a class Student using a constructor function. Then we create an instance of this class and store it to the variable jim . Code Block |
---|
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)); |
new is often used with the built-in Date type to get the current day, year, etc.
Code Block |
---|
var currentTime = new Date();
var year = currentTime.getFullYear(); |
|
delete
| Delete Property | delete object.property
| Deletes a property from an object. In the example below, we delete the firstName property from the object employee . Code Block |
---|
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 pracice, this can be used to perform multiple actions at the end of a for loop. The example below contains a for loop with two variables i and j . i increases by 1 each loop, while j decreases by 1. Code Block |
---|
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); |
|
?
| Ternary Comparison | 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 where each case has a value associated with it. Code Block |
---|
var isOn = tag.read("motorOn");
var s = isOn ? "Motor is on." : "Motor is off.";
notification.send(s); |
This is equivalent to the script below. Code Block |
---|
var isOn = tag.read("motorOn");
if (isOn) {
var s = "Motor is on.";
} else {
var s = "Motor is off.";
}
notification.send(s); |
|