TypeScript - 关系运算符示例



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

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

运算符 描述 示例
> 大于 (A > B) 为假
< 小于 (A < B) 为真
>= 大于或等于 (A >= B) 为假
<= 小于或等于 (A <= B) 为真
== 相等 (A == B) 为假
!= 不相等 (A != B) 为真

示例

var num1:number = 5;
var num2:number = 9;

console.log("Value of num1: "+num1); 
console.log("Value of num2 :"+num2);
 
var res = num1>num2 
console.log("num1 greater than num2: "+res)

res = num1<num2 
console.log("num1 lesser than num2: "+res)  

res = num1>=num2 
console.log("num1 greater than or equal to  num2: "+res)

res = num1<=num2
console.log("num1 lesser 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)

以上代码的转译 JS 版本 -

var num1 = 5; 
var num2 = 9; 
console.log("Value of num1: " + num1); 
console.log("Value of num2 :" + num2); 

var res = num1 > num2; 
console.log("num1 greater than num2: " + res); 

res = num1 < num2; 
console.log("num1 lesser than num2: " + res); 

res = num1 >= num2; 
console.log("num1 greater than or equal to  num2: " + res); 

res = num1 <= num2; 
console.log("num1 lesser than or equal to num2: " + res); 

res = num1 == num2; 
console.log("num1 is equal to num2: " + res); 

res = num1 != num2; 
console.log("num1 not equal to  num2: " + res);

它将产生以下输出 -

Value of num1: 5 
Value of num2 :9 
num1 greater than num2: false 
num1 lesser than num2: true 
num1 greater than or equal to num2: false 
num1 lesser than or equal to num2: true 
14 num1 is equal to num2: false 
16 num1 not equal to num2: true
typescript_operators.htm
广告