In this section, we will talk TypeScript
the use of operators in 0612e4b03b0693. We should all know the operators. We have also learned operators in mathematics, such as common addition, subtraction, multiplication, and division. Operators in computer languages are used to perform program code calculations, which will perform operations on more than one operand item, such as 1 + 2
, where +
is an operator, and 1、2
is an operand. TypeScript
the operator would roughly JavaScript
the same.
TypeScript
can be roughly divided into the following types:
- Arithmetic Operator
- Relational operator
- Logical Operators
- Bitwise operator
- Assignment operator
- Ternary operator
- Type operator
Arithmetic Operator
Arithmetic operators take values (literals or variables) as their operands and return a single value. Including the following:
Operator | describe |
---|---|
+ | Addition, return the sum of the operands |
- | Subtraction, returns the difference of the operands |
* | Multiplication, returns the product of the operands |
/ | Division, returns the quotient of the operand |
% | Take the modulus (remainder) and return the remainder of the operand |
++ | Increment, add 1 to the operand |
-- | Decrement, subtract 1 from the operand |
Example:
Among the above arithmetic operators, the simplest are the four operators +
, -
, *
, /
console.log(1 + 3); // 4
console.log(5 - 2); // 3
console.log(3 * 7); // 21
console.log(6 / 2); // 3
The modulus operator %
used to find the remainder of the operand. The remainder is when the operand cannot be divided evenly, it will produce the remainder. If it can be an integer, it will return 0:
console.log(6 % 2); // 0
console.log(7 % 4); // 3
console.log(9 % 2); // 1
The increment operator ++
and the decrement operator --
are also often used in practical applications. The two operators are used in the same way. The difference is that one is used to increase the operand by one, and the other is used to subtract one. We use ++
as an example:
let a:number = 1;
console.log(a); // 1
console.log(a++); // 1
console.log(a); // 2
console.log(++a); // 3
console.log(a); // 3
In the above code, we can see:
- When
++
when the rear, i.e. behind the operand, will before incrementing return value. - When
++
when the front, i.e. in front of the operand, will after incrementing return value.
Relational operator
The relational operator is used to calculate whether the result is true
or false
. There are 6 types of relational operators, as follows:
Operator | describe |
---|---|
> | more than the |
< | Less than |
>= | greater than or equal to |
<= | less than or equal to |
== | equal |
!= | not equal to |
Example:
Declare two numeric variables, then use different relational operators to calculate the two variables, and return the calculation result:
let a:number = 3;
let b:number = 7;
console.log(a < b); // true
console.log(a > b); // false
console.log(a <= b); // true
console.log(a >= b); // false
console.log(a == b); // false
console.log(a != b); // true
Logical Operators
Logical operators are used to combine two or more conditions. The return value is true
or false
.
Operator | describe | ||
---|---|---|---|
&& | And, the operator returns true only when all the specified expressions return true | ||
\ | \ | Or, if at least one of the specified expressions returns true, the operator returns true | |
! | Not, the operator returns the opposite expression result |
Example:
Let's look at the following code:
let a:number = 3;
let b:number = 7;
console.log(a > 1 && b > 1);
console.log(a == 3 || b == 3);
console.log(!a);
Compile into JavaScript code:
var a = 3;
var b = 7;
console.log(a > 1 && b > 1);
console.log(a == 3 || b == 3);
console.log(!a);
Output:
true
true
false
In fact, the difference between these three operators can be seen from the names of these three operators. The operator &&
requires all expressions to be true
before returning true
. In the above code, a > 1
result is to true, b > 1
results also true
, so the final a > 1 && b > 1
returned result is true.
Or the operator ||
long as a result of the expression is true is returned true
, a == 3
is true, b == 3
is false, there is a meet, so eventually return true
.
Non-operator !
expressions opposite result is returned, a
value 3
, it is a true value, the negated give false.
Bitwise operator
Bit operations are unary and binary operations of bitwise or binary numbers in the bit mode in program design.
Operator | describe | |
---|---|---|
& | Bitwise AND, processing two binary numbers of the same length, and the two corresponding binary bits are both 1, and the result value of the bit is 1, otherwise it is 0 | |
\ | Bitwise OR, processing two binary numbers of the same length, as long as one of the two corresponding binary bits is 1, the result value of this bit is 1. | |
^ | Bitwise XOR: Perform logical XOR operation on the bitwise or each bit of the binary number in the equal-length binary mode. If a bit is different, the bit is 1, otherwise the bit is 0 | |
〜 | The bitwise negation is a unary operator, which performs the logical inversion operation on each bit of a binary number. Make the number 1 become 0 and 0 become 1 | |
<< | Shift left, the binary bits of the operand on the left are all shifted to the left by several bits, the number of shifts is specified by the number on the right, the high bits are discarded, and the low bits are filled with 0 | |
>> | Shift right, shift all the binary digits of the operand on the left of >> by several digits, and the number on the right of >> specifies the number of digits to move | |
>>> | Unsigned right shift is similar to signed right shift, except that the left side always uses 0 to fill. |
Example:
Let's take a look at the following piece of code:
let a:number = 3;
let b:number = 7;
console.log(a & 1); // 1
console.log(a | b); // 7
console.log(a ^ b); // 4
console.log(~a); // -4
console.log(a << b); // 384
console.log(a >> b); // 0
console.log(a >>> b); // 0
According to the description of each operator, it is actually easy to get the result. For example, a & 1
is to convert the values of 3 and 1 into binary, and then the value of the same position is 1, and the value of this bit is 1, otherwise it is 0, as follows:
3的二进制:00000011
1的二进制:00000001
3&1 得到:00000001
00000001化为十进制,就是1,所以a & 1的结果为1
Another example is a | b
, which converts 3 into binary 00000011
and 7 into binary 00000111
. As long as one of the same positions is 1, the value of this position is 1, and the final result is 00000111
, and the result is converted to decimal, so a | b
The final result is 7.
Assignment operator
Assignment operators are used to assign values to variables. The most common one should be the equal sign =
, which we have always used when declaring variables. Other assignment operators include:
Operator | describe |
---|---|
= | Assignment, assign the value from the right operand to the left operand |
+= | Addition assignment, it adds the right operand to the left operand and assigns the result to the left operand. |
-= | Subtraction assignment, it subtracts the right operand from the left operand and assigns the result to the left operand. |
*= | Multiplication assignment, it multiplies the right operand with the left operand, and assigns the result to the left operand. |
/= | Division assignment, it divides the left operand by the right operand and assigns the result to the left operand. |
Example:
=
is the simplest assignment operator, which assigns the value on the right side of the symbol to the variable on the left. For example, the following code assigns 10 to the variable a:
let a:number;
a = 10;
console.log(a); // 10
The assignment of addition, subtraction, multiplication, and division is also easy to understand, which is equivalent to adding, subtracting, multiplying, and dividing a number on the right on the basis of the variable on the left:
a += 2;
console.log(a); // 12
a -= 5;
console.log(a); // 7
a *= 10;
console.log(a); // 70
a /= 10;
console.log(a); // 7
For example, a += 2
is equivalent to a = a + 2
, a -= 5
is equivalent to a = a - 5
, and others can be deduced by analogy.
Ternary operator
The ternary operation has 3 operands, and the value of the Boolean expression needs to be judged. The main point of this operator is to determine which value should be assigned to the variable.
Test ? expr1 : expr2
Which Test
conditions specified in the statement, if the conditional statement Test
is true
returns expr1
, as false
returns expr2
.
Example:
The value of the variable a is 7. We use the ternary operator to determine whether a is greater than 5, if yes, output "a greater than 5", otherwise output "a less than or equal to 5":
let a:number = 7;
console.log(a > 5 ? "a大于5" : "a小于等于5");
// 输出:a大于5
Type operator
typeof
is a unary operator that can be used to return the data type of the operand.
Example:
Declare and initialize three variables
let a = 1;
let b = "xkd";
let c = true;
Then output the data types of these three variables typeof
console.log(typeof a); // number
console.log(typeof b); // string
console.log(typeof c); // boolean
Hands-on practice
1. There is a string type variable username
, please assign an initial value to this variable?
2. It is known that the value of the a
399
, and the value of the variable b
is 25
, please output the result of a % b
3. Please use the ternary operator to judge whether the value of a certain variable is "summer", if yes, output true
, if not, output false
?
4. Please judge the data types of the following variables and output the results:
var arr = [1, 2, 3, 4, 5];
var obj = {'a':1};
var num = 100;
var str = "侠课岛";
Link: https://www.9xkd.com/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。