TypeScript 运算符

2024年8月29日 | 阅读 7 分钟

运算符是作用于值或数据的符号。它表示对数据执行的特定操作。运算符所操作的数据称为操作数。它可用于一个或多个值以产生单个值。所有标准的 JavaScript 运算符都可用于 TypeScript 程序。

示例

在上面的例子中,值 '10' 和 '20' 称为操作数,而 '+' 和 '=' 称为运算符。

TypeScript 中的运算符

在 TypeScript 中,运算符可以按以下方式进行分类。


算术运算符

算术运算符将数值作为其操作数,执行一个操作,然后返回一个单个数值。最常见的算术运算符是加法 (+)、减法 (-)、乘法 (*) 和除法 (/)。

运算符运算符名称描述示例
+加法它返回值的总和。
let a = 20;
let b = 30;
let c = a + b;
console.log( c ); //
Output
30
-减法它返回值的差。
let a = 30;
let b = 20;
let c = a - b;
console.log( c ); //
Output
10
*乘法它返回值的乘积。
let a = 30;
let b = 20;
let c = a * b;
console.log( c ); //
Output
600
/除法它执行除法运算,并返回商。
let a = 100;
let b = 20;
let c = a / b;
console.log( c ); //
Output
5
%取模它执行除法运算并返回余数。
let a = 95;
let b = 20;
let c = a % b;
console.log( c ); //
Output
15
++递增它用于将变量的值增加 1。
let a = 55;
a++;
console.log( a ); //
Output
56
--递减它用于将变量的值减少 1。
let a = 55;
a--;
console.log( a ); //
Output
54

比较(关系)运算符

比较运算符用于比较两个操作数。这些运算符返回布尔值 true 或 false。重要的比较运算符如下所示。

运算符运算符名称描述示例
==等于它检查两个操作数的值是否相等。
let a = 10;
let b = 20;
console.log(a==b);     //false
console.log(a==10);    //true
console.log(10=='10'); //true
===相同(相等且类型相同)它检查两个操作数的类型和值是否相等。
let a = 10;
let b = 20;
console.log(a===b);    //false
console.log(a===10);   //true
console.log(10==='10'); //false
!=不等于它检查两个操作数的值是否相等。
let a = 10;
let b = 20;
console.log(a!=b);     //true
console.log(a!=10);    //false
console.log(10!='10'); //false
!==不相同它检查两个操作数的类型和值是否相等。
let a = 10;
let b = 20;
console.log(a!==b);     //true
console.log(a!==10);    /false
console.log(10!=='10'); //true
>大于它检查左操作数的值是否大于右操作数的值。
let a = 30;
let b = 20;
console.log(a>b);     //true
console.log(a>30);    //false
console.log(20> 20'); //false
>=大于等于它检查左操作数的值是否大于或等于右操作数的值。
let a = 20;
let b = 20;
console.log(a>=b);     //true
console.log(a>=30);    //false
console.log(20>='20'); //true
<小于它检查左操作数的值是否小于右操作数的值。
let a = 10;
let b = 20;
console.log(a<b);      //true
console.log(a<10);     //false
console.log(10<'10');  //false
<=小于等于它检查左操作数的值是否小于或等于右操作数的值。
let a = 10;
let b = 20;
console.log(a<=b);     //true
console.log(a<=10);    //true
console.log(10<='10'); //true

逻辑运算符

逻辑运算符用于将两个或多个条件组合成一个表达式,并返回布尔结果 true 或 false。逻辑运算符如下所示。

运算符运算符名称描述示例
&&逻辑与如果两个操作数(表达式)都为 true,则返回 true,否则返回 false。
let a = false;
let b = true;
console.log(a&&b);      /false
console.log(b&&true);   //true
console.log(b&&10);     //10 which is also 'true'
console.log(a&&'10');  //false
||逻辑或如果任何一个操作数(表达式)为 true,则返回 true,否则返回 false。
let a = false;
let b = true;
console.log(a||b);      //true
console.log(b||true);   //true
console.log(b||10);     //true
console.log(a||'10');   //'10' which is also 'true'
!逻辑非它返回操作数(表达式)的逆结果。
let a = 20;
let b = 30;
console.log(!true);    //false
console.log(!false);   //true
console.log(!a);       //false
console.log(!b);       /false
console.log(!null);    //true

按位运算符

位运算符对操作数执行位运算。位运算符如下所示。

运算符运算符名称描述示例
&按位与它返回对其整数参数的每个位的布尔 AND 运算的结果。
let a = 2;
let b = 3;
let c = a & b;
console.log(c);   //
Output 2
|按位或它返回对其整数参数的每个位的布尔 OR 运算的结果。
let a = 2;
let b = 3;
let c = a | b;
console.log(c);   //
Output 3
^按位异或它返回对其整数参数的每个位的布尔异或运算的结果。
let a = 2;
let b = 3;
let c = a ^ b;
console.log(c);   //
Output 
1
~按位非 (Bitwise NOT)它反转操作数的每个位。
let a = 2;
let c = ~ a;
console.log(c);   //
Output 
-3
>>按位右移左操作数的值向右移动右操作数中指定的位数。
let a = 2;
let b = 3;
let c = a >> b;
console.log(c);   //
Output 
0
<<按位左移左操作数的值向左移动右操作数中指定的位数。新位在右侧填充零。
let a = 2;
let b = 3;
let c = a << b;
console.log(c);   //
Output 
16
>>>带零的位右移左操作数的值向右移动右操作数中指定的位数,并在左侧添加零。
let a = 3;
let b = 4;
let c = a >>> b;
console.log(c);   //
Output 
0

赋值运算符

赋值运算符用于将值分配给变量。赋值运算符的左侧称为变量,赋值运算符的右侧称为值。变量和值的数据类型必须相同,否则编译器将抛出错误。赋值运算符如下所示。

运算符运算符名称描述示例
=赋值它将右侧的值分配给左侧操作数。
let a = 10;
let b = 5;
console.log("a=b:" +a);   //
Output 
10
+=加法并赋值它将左操作数与右操作数相加,并将结果分配给左侧操作数。
let a = 10;
let b = 5;
let c = a += b;
console.log(c);   //
Output 
15
-=减法并赋值它从左操作数中减去右操作数,并将结果分配给左侧操作数。
let a = 10;
let b = 5;
let c = a -= b;
console.log(c);   //
Output 
5
*=乘法并赋值它将左操作数与右操作数相乘,并将结果分配给左侧操作数。
let a = 10;
let b = 5;
let c = a *= b;
console.log(c);   //
Output 
50
/=除法并赋值它将左操作数除以右操作数,并将结果分配给左侧操作数。
let a = 10;
let b = 5;
let c = a /= b;
console.log(c);   //
Output 
2
%=取模并赋值它将左操作数除以右操作数,并将结果分配给左侧操作数。
let a = 16;
let b = 5;
let c = a %= b;
console.log(c);   //
Output 
1

三元/条件运算符

条件运算符接受三个操作数,并根据条件返回一个布尔值,即 true 或 false。它的工作方式类似于 if-else 语句。条件运算符具有从右到左的关联性。条件运算符的语法如下所示。

  • 表达式:它指的是条件表达式。
  • expression-1:如果条件为真,则将返回 expression-1。
  • expression-2:如果条件为假,则将返回 expression-2。

示例

输出

True

连接运算符

连接 (+) 运算符是用于追加两个字符串的运算符。在连接操作中,我们不能在字符串之间添加空格。我们可以在单个语句中连接多个字符串。以下示例帮助我们理解 TypeScript 中的连接运算符。

示例

输出

Result of String Operator: Welcome to JavaTpoint

类型运算符

有一组运算符可帮助您在 TypeScript 中使用对象。诸如 typeof、instanceof、in 和 delete 这样的运算符都是类型运算符的例子。这些运算符的详细解释如下所示。

运算符名称描述示例
in它用于检查对象上是否存在属性。
let Bike = {make: 'Honda', model: 'CLIQ', year: 2018};
console.log('make' in Bike);   // 
Output:
true
删除它用于从对象中删除属性。
let Bike = { Company1: 'Honda',
             Company2: 'Hero',
             Company3: 'Royal Enfield'
           };
delete Bike.Company1;
console.log(Bike);   // 
Output:
{ Company2: 'Hero', Company3: 'Royal Enfield' }
typeof它返回操作数的数据类型。
let message = "Welcome to " + "JavaTpoint";
console.log(typeof message);  // 
Output:
String
instanceof它用于检查对象是否为指定的类型。
let arr = [1, 2, 3];
console.log( arr instanceof Array ); // true
console.log( arr instanceof String ); // false