TypeScript 比较运算符

TypeScript 比较运算符

原文: https://howtodoinjava.com/typescript/comparison-operators/

TypeScript 比较运算符与 JavaScript 相同。 比较运算符可帮助比较两个变量的值。

请注意,有些运算符在比较值时会使用类型转换,而有些则不会。 请小心使用它们。

类型转换表示,当一个运算符的操作数为不同类型时,其中一个将转换为另一个操作数类型的“等效”值。

例如:

100 == 	"100" //true
100 === "100" //false

比较运算符

==

  1. 检查两个操作数的值是否相等
  2. 该运算符使用强制转换。
			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar == secondVar );	//false
			console.log( firstVar == '10' );		//true
			console.log( 10 == '10' );				//true

===

  1. 检查两个操作数的值和类型是否相等。
  2. 该运算符不使用强制转换。
			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar === secondVar );	//false
			console.log( firstVar === 10 );			//true
			console.log( firstVar === '10' );		//false
			console.log( 10 === '10' );				//false

!=

  1. 检查两个操作数的值是否相等
  2. 该运算符使用强制转换。
			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar != secondVar );	//true
			console.log( firstVar != 10 );			//false
			console.log( firstVar != '10' );		//false

!==

  1. 检查两个操作数的值和类型是否相等。
  2. 该运算符使用强制转换。
			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar !== secondVar );	//true
			console.log( firstVar !== 10 );			//false
			console.log( firstVar !== '10' );		//true

>

检查左操作数的值是否大于右操作数的值。 该运算符使用强制转换。

			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar > secondVar );	//false
			console.log( firstVar > 10 );			//false
			console.log( firstVar > '10' );			//false

<

检查左操作数的值是否小于右操作数的值。 该运算符使用强制转换。

			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar < secondVar );	//true
			console.log( firstVar < 10 );			//false
			console.log( firstVar < '10' );			//false

>=

检查左操作数的值是否大于等于右操作数的值。 该运算符使用强制转换。

			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar >= secondVar );	//false
			console.log( firstVar >= 10 );			//true
			console.log( firstVar >= '10' );		//true

<=

检查左操作数的值是否小于等于右操作数的值。 该运算符使用强制转换。

			let firstVar = 10;
			let secondVar = 20;

			console.log( firstVar <= secondVar );	//true
			console.log( firstVar <= 10 );			//true
			console.log( firstVar <='10' );			//true

将我的问题放在评论部分。

学习愉快!