TypeScript 运算符

文章目录

``````7 + 5 = 12
``````

TypeScript 主要包含以下几种运算：

1. 算术运算符
2. 逻辑运算符
3. 关系运算符
4. 按位运算符
5. 赋值运算符
6. 三元/条件运算符
7. 字符串运算符
8. 类型运算符

算术运算符

``````var num1:number = 10
var num2:number = 2
var res:number = 0
res = num1 + num2
console.log("加: "+res);
res = num1 - num2;
console.log("减: "+res)
res = num1*num2
console.log("乘: "+res)
res = num1/num2
console.log("除: "+res)
res = num1%num2
console.log("余数: "+res)
num1++
console.log("num1 自增运算: "+num1)
num2--
console.log("num2 自减运算: "+num2)
``````

``````var num1 = 10;
var num2 = 2;
var res = 0;
res = num1 + num2;
console.log("加: " + res);
res = num1 - num2;
console.log("减: " + res);
res = num1 * num2;
console.log("乘: " + res);
res = num1 / num2;
console.log("除: " + res);
res = num1 % num2;
console.log("余数: " + res);
num1++;
console.log("num1 自增运算: " + num1);
num2--;
console.log("num2 自减运算: " + num2);
``````

``````加:        12

num1 自增运算: 11
num2 自减运算: 1
``````

关系运算符

x=5，下面的表格解释了关系运算符的操作：

``````var num1:number = 5;
var num2:number = 9;
console.log("num1 的值为: "+num1);
console.log("num2 的值为:"+num2);
var res = num1>num2
console.log("num1 大于n num2: "+res)
res = num1<num2
console.log("num1 小于 num2: "+res)
res = num1>=num2
console.log("num1 大于或等于 num2: "+res)
res = num1<=num2
console.log("num1 小于或等于 num2: "+res)
res = num1==num2
console.log("num1 等于 num2: "+res)
res = num1!=num2
console.log("num1 不等于 num2: "+res)
``````

``````var num1 = 5;
var num2 = 9;
console.log("num1 的值为: " + num1);
console.log("num2 的值为:" + num2);
var res = num1 > num2;
console.log("num1 大于n num2: " + res);
res = num1 < num2;
console.log("num1 小于 num2: " + res);
res = num1 >= num2;
console.log("num1 大于或等于 num2: " + res);
res = num1 <= num2;
console.log("num1 小于或等于 num2: " + res);
res = num1 == num2;
console.log("num1 等于 num2: " + res);
res = num1 != num2;
console.log("num1 不等于 num2: " + res);
``````

``````num1 的值为: 5
num2 的值为:9
num1 大于n num2: false
num1 小于 num2: true
num1 大于或等于  num2: false
num1 小于或等于 num2: true
num1 等于 num2: false
num1 不等于 num2: true
``````

逻辑运算符

``````var avg:number = 20;
var percentage:number = 90;
console.log("avg 值为: "+avg+" ,percentage 值为: "+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);
``````

``````var avg = 20;
var percentage = 90;
console.log("avg 值为: " + avg + " ,percentage 值为: " + 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);
``````

``````avg 值为: 20 ,percentage 值为: 90
(avg>50)&&(percentage>80):  false
(avg>50)||(percentage>80):  true
!((avg>50)&&(percentage>80)):  true
``````

短路运算符(&& 与 ||)

&& 与 || 运算符可用于组合表达式。 && 运算符只有在左右两个表达式都为 true 时才返回 true。

``````var a = 10
var result = ( a<10 && a>5)
``````

|| 运算符只要其中一个表达式为 true ，则该组合表达式就会返回 true。

``````var a = 10
var result = ( a>5 || a<10)
``````

位运算符

``````var a:number = 2;   // 二进制 10
var b:number = 3;   // 二进制 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);
``````

``````var a = 2; // 二进制 10
var b = 3; // 二进制 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);
``````

``````(a & b) =>  2
(a | b) =>  3
(a ^ b) =>  1
(~b) =>  -4
(a << b) =>  16
(a >> b) =>  0
(a >>> 1) =>  1
``````

赋值运算符

``````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)
``````

``````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);
``````

``````a = b: 10
a+=b: 20
a-=b: 10
a*=b: 100
a/=b: 10
a%=b: 0
``````

三元运算符 (?)

``````Test ? expr1 : expr2
``````
1. Test − 指定的条件语句
2. expr1 − 如果条件语句 Test 返回 true 则返回该值
3. expr2 − 如果条件语句 Test 返回 false 则返回该值
让我们看下以下实例：
``````var num:number = -2
var result = num > 0 ? "大于 0" : "小于 0，或等于 0"
console.log(result)
``````

``````var num = -2;
var result = num > 0 ? "大于 0" : "小于 0，或等于 0";
console.log(result);
``````

``````小于 0，或等于 0
``````

类型运算符

typeof 运算符

typeof 是一元运算符，返回操作数的数据类型。

``````var num = 12
console.log(typeof num);   //输出结果: number
``````

``````var num = 12;
console.log(typeof num); //输出结果: number
``````

``````number
``````

instanceof

instanceof 运算符用于判断对象是否为指定的类型，后面章节我们会具体介绍它。

其他运算符

负号运算符(-)

``````var x:number = 4
var y = -x;
console.log("x 值为: ",x);   // 输出结果 4
console.log("y 值为: ",y);   // 输出结果 -4
``````

``````var x = 4;
var y = -x;
console.log("x 值为: ", x); // 输出结果 4
console.log("y 值为: ", y); // 输出结果 -4
``````

``````x 值为:  4
y 值为:  -4
``````

字符串运算符: 连接运算符 (+)

• 运算符可以拼接两个字符串，查看以下实例：
``````var msg:string = "RUNOOB"+".COM"
console.log(msg)
``````

``````var msg = "RUNOOB" + ".COM";
console.log(msg);
``````

``````RUNOOB.COM
``````

原文作者：X W F
原文地址: https://blog.csdn.net/weixin_46399258/article/details/104689039
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。