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 to add values.
The = operator is used for assignment.
TypeScript mainly includes the following kinds of operations:
- Arithmetic operators
- Logical operators
- Relational operators
- Bitwise Operators
- Assignment operators
- Ternary/Conditional Operator
- String operators
- Type Operator
Arithmetic operators
Assuming y=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
num1
:
number
=
10
var
num2
:
number
=
2
var
res
:
number
=
0
res
=
num1
num2
console.
log
(
"Add: "
res
)
;
res
=
num1
num2
;
console.
log
(
"Subtract: "
res
)
res
=
num1
num2
console.
log
(
"Multiply: "
res
)
res
=
num1
/
num2
console.
log
(
"Divide: "
res
)
res
=
num1
%
num2
console.
log
(
"Remainder: "
res
)
num1
++
console.
log
(
"num1 increment: "
num1
)
num2
--
console.
log
(
"num2 decrement: "
num2
)
Compile the above code with the tsc command to get the following JavaScript code:
var
num1
=
10
;
var
num2
=
2
;
var
res
=
0
;
res
=
num1
num2
;
console
.
log
(
"
Add:
"
res
)
;
res
=
num1
num2
;
console
.
log
(
"
Subtract:
"
res
)
;
res
=
num1
num2
;
console
.
log
(
"
Multiply:
"
res
)
;
res
=
num1
/
num2
;
console
.
log
(
"
Divide:
"
res
)
;
res
=
num1
%
num2
;
console
.
log
(
"
Remainder:
"
res
)
;
num1
++;
console
.
log
(
"
num1 increment:
"
num1
)
;
num2
--;
console
.
log
(
"
num2 decrement:
"
num2
)
;
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 compute whether the result is true or false.
x=5, the following table explains the operations of relational operators:
| operator | Description | Comparison | Return Value |
|---|---|---|---|
| == | Equal | x==8 | false |
| x==5 | true | ||
| != | Not equal | 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
var
num1
:
number
=
5
;
var
num2
:
number
=
9
;
console
.
log
(
"
The value of num1 is:
"
num1
)
;
console
.
log
(
"
The value of num2 is:
"
num2
)
;
var
res
=
num1
num2
console
.
log
(
"
num1 is greater than n num2:
"
res
)
res
=
num1
<
num2
console.
log
(
"
num1 is less than num2:
"
res
)
res
=
num1
=
num2
console
.
log
(
"
num1 is greater than or equal to num2:
"
res
)
res
=
num1
<=
num2
console
.
log
(
"
num1 is less than or equal to num2:
"
res
)
res
=
num1
==
num2
console
.
log
(
"
num1 is equal to num2:
"
res
)
res
=
num1
!=
num2
console
.
log
(
"
num1 is not equal to num2:
"
res
)
Compile the above code with the tsc command to get the following JavaScript code:
var
num1
=
5
;
var
num2
=
9
;
console
.
log
(
"
The value of num1 is:
"
num1
)
;
console
.
log
(
"
The value of num2 is:
"
num2
)
;
var
res
=
num1
num2
;
console
.
log
(
"
num1 is greater than n num2:
"
res
)
;
res
=
num1
<
num2
;
console
.
log
(
"
num1 is less than num2:
"
res
)
;
res
=
num1
=
num2
;
console
.
log
(
"
num1 is greater than or equal to num2:
"
res
)
;
res
=
num1
<=
num2
;
console
.
log
(
"
num1 is less than or equal to num2:
"
res
)
;
res
=
num1
==
num2
;
console
.
log
(
"
num1 is equal to num2:
"
res
)
;
res
=
num1
!=
num2
;
console
.
log
(
"
num1 is not equal to num2:
"
res
)
;
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 table below 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
var
avg
:
number
=
20
;
var
percentage
:
number
=
90
;
console
.
log
(
"
The avg value is:
"
avg
"
,the percentage value is:
"
percentage
)
;
var
res
:
boolean
=
(
(
avg
50
)
&&
(
percentage
80
)
)
;
console
.
log
(
"
(avg>50)&&(percentage>80):
"
,
res
)
;
var
res
:
boolean
=
(
(
avg
50
)
||
(
percentage
80
)
)
;
console
.
log
(
"
(avg>50)||(percentage>80):
"
,
res
)
;
var
res
:
boolean
=!
(
(
avg
50
)
&&
(
percentage
80
)
)
;
console
.
log
(
"
!((avg>50)&&(percentage>80)):
"
,
res
)
;
Compile the above code with the tsc command to get the following JavaScript code:
var
avg
=
20
;
var
percentage
=
90
;
console
.
log
(
"
The avg value is:
"
avg
"
,the percentage value is:
"
percentage
)
;
var
res
=
(
(
avg
50
)
&&
(
percentage
80
)
)
;
console
.
log
(
"
(avg>50)&&(percentage>80):
"
,
res
)
;
var
res
=
(
(
avg
50
)
||
(
percentage
80
)
)
;
console
.
log
(
"
(avg>50)||(percentage>80):
"
,
res
)
;
var
res
= !
(
(
avg
50
)
&&
(
percentage
80
)
)
;
console
.
log
(
"
!((avg>50)&&(percentage>80)):
"
,
res
)
;
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 judgment (a > 5) is skipped 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 judgment (a < 10) is skipped and true is returned directly.
Bitwise operators
Bitwise operations in programming are unary and binary operations on bit patterns or binary numbers.
| operator | Description | Example | Similar to | Result | Decimal |
|---|---|---|---|---|---|
| & | AND, bitwise AND operates on two binary numbers of equal length; the result for a bit is 1 only when both corresponding bits are 1, otherwise it is 0. | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | | OR, bitwise OR operates on two binary numbers of equal length; the result for a bit is 1 as long as one of the two corresponding bits is 1. | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
| ~ | NOT (complement), a unary operator that performs a logical negation on each bit of a binary number, turning 1 into 0 and 0 into 1. | x = ~ 5 | ~0101 | 1010 | -6 |
| ^ | XOR, bitwise exclusive OR, performs a logical exclusive OR on each bit of two equal-length bit patterns or binary numbers. The result is 1 if the bits differ, otherwise it is 0. | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
| << | Left shift, shifts << all bits of the left operand left by several positions, with the << number on the right specifying the number of positions to shift; high bits are discarded and low bits are padded with 0. | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
| >> | Right shift, shifts all bits of the operand to the left of >> right by several positions, with the number to the right of >> specifying the number of positions to shift. | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |
| >>> | Unsigned right shift, similar to signed right shift, except that the left side is always padded with 0. | x = 2 >>> 1 | 0010 >>> 1 | 0001 | 1 |
Example
var
a
:
number
=
2
;
//
Binary 10
var
b
:
number
=
3
;
//
Binary 11
var
result
;
result
=
(
a
&
b
)
;
console
.
log
(
"
(a & b) =>
"
,
result
)
result
=
(
a
|
b
)
;
console
.
log
(
"
(a | b) =>
"
,
result
)
result
=
(
a
^
b
)
;
console
.
log
(
"
(a ^ b) =>
"
,
result
)
;
result
=
(
~
b
)
;
console
.
log
(
"
(~b) =>
"
,
result
)
;
result
=
(
a
<<
b
)
;
console
.
log
(
"
(a << b) =>
"
,
result
)
;
result
=
(
a
b
)
;
console
.
log
(
"
(a >> b) =>
"
,
result
)
;
result
=
(
a
1
)
;
console
.
log
(
"
(a >>> 1) =>
"
,
result
)
;
Compile the above code with the tsc command to get the following JavaScript code:
var
a
=
2
;
//
Binary 10
var
b
=
3
;
//
Binary 11
var
result
;
result
=
(
a
&
b
)
;
console
.
log
(
"
(a & b) =>
"
,
result
)
;
result
=
(
a
|
b
)
;
console
.
log
(
"
(a | b) =>
"
,
result
)
;
result
=
(
a
^
b
)
;
console
.
log
(
"
(a ^ b) =>
"
,
result
)
;
result
=
(
~
b
)
;
console
.
log
(
"
(~b) =>
"
,
result
)
;
result
=
(
a
<<
b
)
;
console
.
log
(
"
(a << b) =>
"
,
result
)
;
result
=
(
a
b
)
;
console
.
log
(
"
(a >> b) =>
"
,
result
)
;
result
=
(
a
1
)
;
console
.
log
(
"
(a >>> 1) =>
"
,
result
)
;
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=10 and y=5, the following table explains the assignment operators:
| operator | Example | Example | x value |
|---|---|---|---|
| = (assignment) | x = y | x = y | x = 5 |
| += (add then assign) | x += y | x = x + y | x = 15 |
| -= (subtract then assign) | x -= y | x = x - y | x = 5 |
| *= (multiply then assign) | x *= y | x = x * y | x = 50 |
| /= (divide then assign) | x /= y | x = x / y | x = 2 |
Similar logical operators can also be combined with assignment operators:<<=, >>=, >>>=, &=, |= The difference between ^=。
Example
var
a
:
number
=
12
var
b
:
number
=
10
a
=
b
console
.
log
(
"
a = b:
"
a
)
a
+=
b
console
.
log
(
"
a+=b:
"
a
)
a
-=
b
console
.
log
(
"
a-=b:
"
a
)
a
*=
b
console
.
log
(
"
a*=b:
"
a
)
a
/=
b
console
.
log
(
"
a/=b:
"
a
)
a
%=
b
console
.
log
(
"
a%=b:
"
a
)
Compile the above code with the tsc command to get the following JavaScript code:
var
a
=
12
;
var
b
=
10
;
a
=
b
;
console
.
log
(
"
a = b:
"
a
)
;
a
+=
b
;
console
.
log
(
"
a+=b:
"
a
)
;
a
-=
b
;
console
.
log
(
"
a-=b:
"
a
)
;
a
*=
b
;
console
.
log
(
"
a*=b:
"
a
)
;
a
/=
b
;
console
.
log
(
"
a/=b:
"
a
)
;
a
%=
b
;
console
.
log
(
"
a%=b:
"
a
)
;
Executing the above JavaScript code produces the following output:
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 a boolean expression. The main purpose of this operator is to decide which value should be assigned to the variable.
Test ? expr1 : expr2
- Test − the specified conditional statement
- expr1 − the value returned if the conditional statement Test returns true
- expr2 − the value returned if the conditional statement Test returns false
Let's look at the following example:
var
num
:
number
= -
2
var
result
=
num
0
?
"
Greater than 0
"
:
"
Less than 0, or equal to 0
"
console
.
log
(
result
)
The example is used to determine whether a variable is greater than 0.
Compile the above code with the tsc command to get the following JavaScript code:
var
num
= -
2
;
var
result
=
num
0
?
"
Greater than 0
"
:
"
Less than 0, or equal to 0
"
;
console
.
log
(
result
)
;
The output of the above example is:
小于 0,或等于 0
Type Operator
typeof operator
typeof is a unary operator that returns the data type of the operand.
Look at the following example:
var
num
=
12
console
.
log
(
typeof
num
)
;
//
Output: number
Compile the above code with the tsc command to get the following JavaScript code:
var
num
=
12
;
console
.
log
(
typeof
num
)
;
//
Output: number
The output of the above example is:
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 minus operator (-)
Changes the sign of the operand; see the following example:
var
x
:
number
=
4
var
y
= -
x
;
console
.
log
(
"
The value of x is:
"
,
x
)
;
//
Output 4
console
.
log
(
"
The value of y is:
"
,
y
)
;
//
Output -4
Compile the above code with the tsc command to get the following JavaScript code:
var
x
=
4
;
var
y
= -
x
;
console
.
log
(
"
The value of x is:
"
,
x
)
;
//
Output 4
console
.
log
(
"
The value of y is:
"
,
y
)
;
//
Output -4
The output of the above example is:
x 值为: 4
y 值为: -4
String operators: concatenation operator (+)
- The operator can concatenate two strings; see the following example:
var
msg
:
string
=
"
RUNOOB
"
"
.COM
"
console
.
log
(
msg
)
Compile the above code with the tsc command to get the following JavaScript code:
var
msg
=
"
RUNOOB
"
"
.COM
"
;
console
.
log
(
msg
)
;
The output of the above example is:
RUNOOB.COM