TypeScript Operators
Operators are used to perform operations on program code, operating on one or more operands.
Consider the following calculation:
7 + 5 = 12
In the above example, 7, 5, and 12 are operands.
The operator+is used for addition.
The operator=is used for assignment.
TypeScript mainly includes the following types of operations:
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Bitwise Operators
- Assignment Operators
- Ternary/Conditional Operators
- String Operators
- Type Operators
Arithmetic Operators
Assumey=5, the following table explains the operations of these arithmetic operators:
| Operator | Description | Example | x Result | y Result |
|---|---|---|---|---|
| + | Addition | x=y+2 | 7 | 5 |
| - | Subtraction | x=y-2 | 3 | 5 |
| * | Multiplication | x=y*2 | 10 | 5 |
| / | Division | x=y/2 | 2.5 | 5 |
| % | Modulus (remainder) | x=y%2 | 1 | 5 |
| ++ | Increment | x=++y | 6 | 6 |
| x=y++ | 5 | 6 | ||
| -- | Decrement | x=--y | 4 | 4 |
| x=y-- | 5 | 4 |
Example
var num2:number = 2
var res:number = 0
res = num1 + num2
console.log("Addition: "+res);
res = num1 - num2;
console.log("Subtraction: "+res)
res = num1*num2
console.log("Multiplication: "+res)
res = num1/num2
console.log("Division: "+res)
res = num1%num2
console.log("Remainder: "+res)
num1++
console.log("num1 increment operation: "+num1)
num2--
console.log("num2 decrement operation: "+num2)
Use thetsctsc command to compile the above code to get the following JavaScript code:
Executing the above JavaScript code produces the following output:
加: 12 减: 8 乘: 20 除: 5 余数: 0 num1 自增运算: 11 num2 自减运算: 1
Relational Operators
Relational operators are used to calculate whether the result is true or false.
x=5, the following table explains the operations of the relational operators:
| Operator | Description | Comparison | Return value |
|---|---|---|---|
| == | Equal to | x==8 | false |
| x==5 | true | ||
| != | Not equal to | x!=8 | true |
| > | Greater than | x>8 | false |
| < | Less than | x<8 | true |
| >= | Greater than or equal to | x>=8 | false |
| <= | Less than or equal to | x<=8 | true |
Example
Use thetsctsc command to compile the above code to get the following JavaScript code:
Executing the above JavaScript code produces the following output:
num1 的值为: 5 num2 的值为:9 num1 大于n num2: false num1 小于 num2: true num1 大于或等于 num2: false num1 小于或等于 num2: true num1 等于 num2: false num1 不等于 num2: true
Logical Operators
Logical operators are used to determine the logic between variables or values.
Given x=6 and y=3, the following table explains the logical operators:
| Operator | Description | Example |
|---|---|---|
| && | and | (x < 10 && y > 1) is true |
| || | or | (x==5 || y==5) is false |
| ! | not | !(x==y) is true |
Example
Use thetsctsc command to compile the above code to get the following JavaScript code:
Executing the above JavaScript code produces the following output:
avg 值为: 20 ,percentage 值为: 90 (avg>50)&&(percentage>80): false (avg>50)||(percentage>80): true !((avg>50)&&(percentage>80)): true
Short-circuit Operators (&& and ||)
The && and || operators can be used to combine expressions. The && operator returns true only when both the left and right expressions are true.
Consider the following example:
var a = 10 var result = ( a<10 && a>5)
In the above example, a < 10 and a > 5 are a combined expression using the && operator. The first expression returns false. Since the && operation requires both expressions to be true, if the first one is false, the subsequent evaluation (a > 5 is skipped) is not executed, and false is returned directly.
The || operator returns true for the combined expression as long as one of the expressions is true.
Consider the following example:
var a = 10 var result = ( a>5 || a<10)
In the above example, a > 5 and a < 10 are a combined expression using the || operator. The first expression returns true. Since the || combined operation only requires one expression to be true, if the first one is true, the subsequent evaluation (a < 10 is skipped) is not executed, and true is returned directly.
Bitwise Operators
Bitwise operations are unary and binary operations on bit patterns or binary numbers in programming.
| Operator | Description | Example | Similar to | Result | Decimal |
|---|---|---|---|---|---|
| & | AND. Bitwise AND processes two binary numbers of the same length. If both corresponding bits are 1, the result bit is 1; otherwise, it is 0. | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | | OR. Bitwise OR processes two binary numbers of the same length. If at least one of the corresponding bits is 1, the result bit is 1. | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
| ~ | NOT. Negation is a unary operator that performs logical negation on each bit of a binary number, turning 1 into 0 and 0 into 1. | x = ~ 5 | ~0101 | 1010 | -6 |
| ^ | XOR. Bitwise XOR performs a logical XOR operation on each bit of equal-length binary patterns or binary numbers. If the bits differ, the result bit is 1; otherwise, it is 0. | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
| << | Left shift. Shifts all bits of the operand to the left of << to the left by the number of bits specified by the operand to the right of <<. High-order bits are discarded and low-order bits are filled with 0. | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
| >> | Right shift. Shifts all bits of the operand to the left of >> to the right by the number of bits specified by the operand to the right of >>. | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |
| >>> | Unsigned right shift. Similar to signed right shift, except that the left side is always filled with 0. | x = 2 >>> 1 | 0010 >>> 1 | 0001 | 1 |
Example
Use thetsctsc command to compile the above code to get the following JavaScript code:
Executing the above JavaScript code produces the following output:
(a & b) => 2 (a | b) => 3 (a ^ b) => 1 (~b) => -4 (a << b) => 16 (a >> b) => 0 (a >>> 1) => 1
Assignment Operators
Assignment operators are used to assign values to variables.
Given x=10x=10 and y=5, the following table explains the assignment operators:
| Operator | Example | Equivalent | x value |
|---|---|---|---|
| = (assignment) | x = y | x = y | x = 5 |
| += (add and assign) | x += y | x = x + y | x = 15 |
| -= (subtract and assign) | x -= y | x = x - y | x = 5 |
| *= (multiply and assign) | x *= y | x = x * y | x = 50 |
| /= (divide and assign) | x /= y | x = x / y | x = 2 |
Similarly, logical operators can also be used in combination with assignment operators:<<=, >>=, >>>=, &=, |=and^=。
Example
Use thetsctsc command to compile the above code to get the following JavaScript code:
Execute the above JavaScript code, the output result is:
a = b: 10 a+=b: 20 a-=b: 10 a*=b: 100 a/=b: 10 a%=b: 0
Ternary Operator (?)
The ternary operation has 3 operands and needs to evaluate the value of a boolean expression. The main purpose of this operator is to decide which value should be assigned to a variable.
Test ? expr1 : expr2
- Test − The specified conditional statement
- expr1 − The value returned if the conditional statement Test evaluates to true
- expr2 − The value returned if the conditional statement Test evaluates to false
Let's look at the following example:
The example is used to determine whether the variable is greater than 0.
Use the tsc command to compile the above code and get the following JavaScript code:
The output of the above example is as follows:
小于 0,或等于 0
Type Operators
typeof Operator
typeof is a unary operator that returns the data type of the operand.
See the following example:
Use the tsc command to compile the above code and get the following JavaScript code:
The output of the above example is as follows:
number
instanceof
The instanceof operator is used to determine whether an object is of a specified type. We will introduce it in detail in later chapters.
Other Operators
Unary Negation Operator (-)
Changes the sign of the operand. See the following example:
Use the tsc command to compile the above code and get the following JavaScript code:
The output of the above example is as follows:
x 值为: 4 y 值为: -4
String Operators: Concatenation Operator (+)
The + operator can concatenate two strings. See the following example:
Use the tsc command to compile the above code and get the following JavaScript code:
The output of the above example is as follows:
example.comOther extensions