TypeScript 关系运算符示例

2018-12-27 15:07 更新

TypeScript  关系运算符示例

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

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

运算符描述示例
>大于(A> B)是 False
<小于(A <B)是 True
> =大于或等于(A> = B)是 False
<=小于或等于(A <= B)是 True
==等于(A == B)是 False
!=不等于(A!= B)是 True

示例

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)

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

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
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号