There are three types of operators that can be used in programs:
Table of Contents |
---|
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).
...
Mathematical Operators
Mathematical operators work on two numbers and return a number.
In these examples, assume A
is 3 and B
is 4.
Operator | Function | Example | Description |
| Bitwise invert |
| Each bit of |
| Addition |
| Variable |
| Subtraction |
| Variable |
| Multiplication |
| Variable |
| Division |
| Variable |
| 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 |
| Shifts the bits of |
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.
...
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 two Boolean values and output a 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 into a Boolean value. In JavaScript, there are 6 values that are evaluated as false
:
false
undefined
null
Nan
(Not a Number)0
...
""
(empty string)
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 |
| If |
>
| Greater than |
| If |
| Less than or equal to |
| If |
| Greater than or equal to |
| If |
| Equal to |
| If |
| Instance of |
| If |
| Is not equal to |
| If |
...
| In |
| If there is an instance of |
Assignment Operators
Operator | Function | Example | Description |
| StoreAssignment |
| Stores the value in variable |
| Addition assignment |
| Stores the value |
...
| |||
| Subtraction assignment |
| Stores the value |
| Increment Operator |
| Adds 1 to the value of |
| Decrement Operator |
| Subtracts 1 from 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 =
operationvariables 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.
All Bitwise operators are performed by converting the inputs into 32-bit signed integer. The 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).
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 |