Comparison operators allow us to compare values and obtain a boolean result (true
or false
) based on the relationship between the values.
These operators enable us to compare two values and determine if they are equal, different, greater, or lesser, and are fundamental for evaluating expressions and making decisions.
The arithmetic operators in JavaScript are:
Operator | Name | Description |
---|---|---|
== | Equality | Compares if two values are equal |
=== | Strict equality | Compares if two values and their type are identical |
!= | Inequality | Compares if two values are different |
!== | Strict inequality | Compares if two values and their type are identical |
> | Greater than | Checks if one value is greater than another |
< | Less than | Checks if one value is less than another |
>= | Greater than or equal to | Checks if one value is greater than or equal |
<= | Less than or equal to | Checks if one value is less than or equal |
If you want to learn more about Comparison Operators
consult the Introduction to Programming Course read more
List of comparison operators
Equality (==)
The equality operator ==
compares two values and returns true
if they are equal, false
if they are different. This operator does not take data type into account.
5 == 5; // true
5 == '5'; // true, the comparison does not consider the type
Strict equality (===)
The strict equality operator ===
compares two values and returns true
if they are equal and of the same type, false
if they are different.
5 === 5; // true
5 === '5'; // false, the types are different
Inequality (!=)
The inequality operator !=
compares two values and returns true
if they are different, false
if they are equal. This operator does not take data type into account.
5 != 3; // true
5 != '5'; // false, the comparison does not consider the type
Strict inequality (!==)
The strict inequality operator !==
compares two values and returns true
if they are different or of different types, false
if they are equal and of the same type.
5 !== 3; // true
5 !== '5'; // true, the types are different
Greater than (>), greater than or equal to (>=)
These operators compare two numeric values and return true
if the first value is greater (or greater than or equal to) than the second, false
if it is not.
5 > 3; // true
5 >= 5; // true
Less than (<), less than or equal to (<=)
These operators compare two numeric values and return true
if the first value is less (or less than or equal to) than the second, false
if it is not.
5 < 10; // true
5 <= 5; // true
It is recommended to use ===
instead of ==
(!==
instead of !=
) and to avoid unexpected behavior due to type coercion.