|Greater than or equals||
|Less than or equals||
|Not (strict) equals||
The result of the comparison is always a boolean value, so either
1 < 3; // => true 2 !== 2; // => false 1 === 1.0; // => true // All numbers are floating-points, so this is different syntax // for the exact same value.
'Apple' > 'Pear'; // => false 'a' < 'above'; // => true 'a' === 'A'; // => false
You need to be careful when you compare two variables that appear to contain numeric values but are of type string.
Due to the dictionary order, the result will not be the same as comparing values of type
10 < 2; // => false '10' < '2'; // => true (because "1" comes before "2")
=== represents the check for strict equality which means that no type conversion is performed and values of different types are always unequal.
'3' === 3; // => false // The value on the left has type string, the value on the right // has type number. 1 === 1n; // => false // The value on the left has type number, the value on the right // has type bigint.
There is also
!= which represents checking for loose equality.
You should avoid it because it will apply implicit type conversion before performing the comparison.
The outcomes in these cases are hard to predict and sometimes not what you would expect.
You can read more about it here.
0 == false; // => true
In theory, you can also compare values of different types (e.g.,
"1" < 2).
Then the values will be implicitly converted to determine whether the result is true or false.
Just as checking for loose equality, this is also not recommended for the same reason as mentioned above.
What should you do instead? You can apply explicit type conversion. With that, you can then ensure values have the correct type before performing the comparison. Then your code will be easier to understand and less error-prone.