Co

Comparison in JavaScript

10 exercises

About Comparison

Comparing Numbers

In JavaScript numbers can be compared using the following relational and equality operators.

Comparison Operator
Greater than a > b
Greater than or equals a >= b
Less than a < b
Less than or equals a <= b
(Strict) Equals a === b
Not (strict) equals a !== b

The result of the comparison is always a boolean value, so either true or false.

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.

Comparing Strings

In JavaScript, the comparison operators above can also be used to compare strings. In that case, a dictionary (lexicographical) order is applied. You can find a list of the exact order of all the characters here.

'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 Number.

10 < 2;
// => false

'10' < '2';
// => true (because "1" comes before "2")

Another way to compare strings is the localeCompare method. It allows setting a variety of options to adjust the way strings are compared.

Strict Equality

You might wonder about the three equal signs for checking equality in JavaScript. === 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.

Using === and !== is the recommended way of checking equality in JavaScript.

Avoiding Implicit Type Conversion

There is also == and != 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.

Edit via GitHub The link opens in a new window or tab

Learn Comparison