Learning TypeScript 2.x
上QQ阅读APP看书,第一时间看更新

Comparison operators

TypeScript supports the following comparison operators. To understand the examples, you must assume that variable A holds 10 as value and variable B holds 20 as value:

Operator

Description

Example

==

Checks whether the values of two operands are equal or not. This operator uses type coercion. If yes, then the condition becomes true.

(A == B) is false. A == "10" is true.

===

Checks whether the value and type of two operands are equal or not. This operator doesn't use type coercion. If yes, then the condition becomes true.

A === B is false. A === "10" is false.

!=

Checks whether the value of two operands are equal or not. If the values are not equal, then the condition becomes true. This operator uses type coercion.

(A != B) is true. A != "10" is false.

!==

Checks whether the value of two operands are equal or not. If the values are not equal, then the condition becomes true. This operator doesn't use type coercion.

A !== B is true. A !== "10" is true.

>

Checks whether the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true.

(A > B) is false.

<

Checks whether the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true.

(A < B) is true.

>=

Checks whether the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.

(A >= B) is false.

<=

Checks whether the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.

(A <= B) is true.