📜  TypeScript-运算符

📅  最后修改于: 2020-10-19 03:49:59             🧑  作者: Mango


什么是运算符?

运算符定义了将在数据上执行的一些函数。运算符作用的数据称为操作数。考虑以下表达式-

7及以上5 = 12

此处,值7、5和12是操作数,而+和=是运算符

TypeScript中的主要运算符可以分类为-

  • 算术运算运算符
  • 逻辑运算符
  • 关系运算符
  • 按位运算符
  • 赋值运算符
  • 三元/条件运算符
  • 字符串运算符
  • 类型运算符

算术运算符

假设变量a和b中的值分别为10和5。

显示范例

Operator Description Example
+ (Addition) returns the sum of the operands a + b is 15
– (Subtraction) returns the difference of the values a – b is 5
* (Multiplication) returns the product of the values a * b is 50
/ (Division) performs division operation and returns the quotient a / b is 2
% (Modulus) performs division operation and returns the remainder a % b is 0
++ (Increment) Increments the value of the variable by one a++ is 11
— (Decrement) Decrements the value of the variable by one a– is 9

关系运算符

关系运算符测试或定义两个实体之间的关系类型。关系运算符返回布尔值,即true / false。

假设A的值为10,B的值为20。

显示范例

Operator Description Example
> Greater than (A > B) is False
< Lesser than (A < B) is True
>= Greater than or equal to (A >= B) is False
<= Lesser than or equal to (A <= B) is True
== Equality (A == B) is false
!= Not equal (A != B) is True

逻辑运算符

逻辑运算符用于组合两个或多个条件。逻辑运算符也返回布尔值。假设变量A的值为10,而B为20。

显示范例

Operator Description Example
&& (And) The operator returns true only if all the expressions specified return true (A > 10 && B > 10) is False
|| (OR) The operator returns true if at least one of the expressions specified return true (A > 10 || B >10) is True
! (NOT) The operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false !(A >10 ) is True

按位运算符

假设变量A = 2和B = 3

显示范例

Operator Description Example
& (Bitwise AND) It performs a Boolean AND operation on each bit of its integer arguments. (A & B) is 2
| (BitWise OR) It performs a Boolean OR operation on each bit of its integer arguments. (A | B) is 3
^ (Bitwise XOR) It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. (A ^ B) is 1
~ (Bitwise Not) It is a unary operator and operates by reversing all the bits in the operand. (~B) is -4
<< (Left Shift) It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on. (A << 1) is 4
>> (Right Shift) Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. (A >> 1) is 1
>>> (Right shift with Zero) This operator is just like the >> operator, except that the bits shifted in on the left are always zero. (A >>> 1) is 1

赋值运算符

显示范例

Operator Description Example
= (Simple Assignment) Assigns values from the right side operand to the left side operand C = A + B will assign the value of A + B into C
+= (Add and Assignment) It adds the right operand to the left operand and assigns the result to the left operand. C += A is equivalent to C = C + A
-= (Subtract and Assignment) It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C – A
*= (Multiply and Assignment) It multiplies the right operand with the left operand and assigns the result to the left operand. C *= A is equivalent to C = C * A
/= (Divide and Assignment) It divides the left operand with the right operand and assigns the result to the left operand.

–相同的逻辑适用于按位运算符,因此它们将变为<< =,>> =,>> =,&=,| =和^ =。

杂项运算符

否定运算符(-)

更改值的符号。让我们举个例子。

var x:number = 4 
var y = -x; 
console.log("value of x: ",x);   //outputs 4 
console.log("value of y: ",y);   //outputs -4

编译时,它将生成以下JavaScript代码。

//Generated by typescript 1.8.10
var x = 4;
var y = -x;
console.log("value of x: ", x);   //outputs 4
console.log("value of y: ", y);   //outputs -4

它将产生以下输出-

value of x:  4 
value of y:  -4

字符串运算符:串联运算符(&plus;)

&plus;运算符应用于字符串,会将第二个字符串附加到第一个字符串。以下示例有助于我们理解这一概念。

var msg:string = "hello"+"world" 
console.log(msg)

编译时,它将生成以下JavaScript代码。

//Generated by typescript 1.8.10
var msg = "hello" + "world";
console.log(msg);

它将产生以下输出-

helloworld

串联操作不会在字符串之间添加空格。可以在单个语句中连接多个字符串。

条件运算符(?)

该运算符用于表示条件表达式。条件运算符有时也称为三元运算符。语法如下所示-

Test ? expr1 : expr2
  • 测试-指条件表达式

  • expr1-如果条件为真,则返回值

  • expr2-如果条件为假,则返回值

让我们看一下下面的代码-

var num:number = -2 
var result = num > 0 ?"positive":"non-positive" 
console.log(result)

第2行检查变量num中的值是否大于零。如果num被设置为一个值大于零,则返回字符串a€œpositiveâ€其他字符串是一个€œnon-positiveâ€返回。

编译时,它将生成以下JavaScript代码。

//Generated by typescript 1.8.10
var num = -2;
var result = num > 0 ? "positive" : "non-positive";
console.log(result);

上面的代码片段将产生以下输出-

non-positive

类型运算符

类型运算符

它是一元运算运算符。该运算符返回操作数的数据类型。看下面的例子-

var num = 12 
console.log(typeof num);   //output: number

编译时,它将生成以下JavaScript代码。

//Generated by typescript 1.8.10
var num = 12;
console.log(typeof num);   //output: number

它将产生以下输出-

number

实例

该运算符可用于测试对象是否为指定类型。使用instanceof运算符的一章中讨论