Comparison Operators

You know comparison operators from math class, but let’s refresh your knowledge:

Greater than operator (a > b) returns true if the left operand is greater than the right operand.

Syntax:

a > b

Example of the greater than operator:

7 > 4 // true

Less than operator (a < b) returns true if the left operand is less than the right operand.

Syntax:

a < b

Example of the less than operator:

4 < 7 // true

Greater than or equal operator (a >= b) returns true if the left operand is greater than or equal to the right operand.

Syntax:

a >= b

Example of the greater than or equal operator:

7 >= 4 // true
4 >= 4 // true

Less than or equal operator (a <= b) returns true if the left operand is less than or equal to the right operand.

Syntax:

a <= b

Example of the less than or equal operator:

4 <= 7 // true
4 <= 4 // true

The equality operator (a == b) converts the operands if they are not of the same type, then applies strict comparison. I both operands are objects, JavaScript compares internal references which are equal when operands refer to the same object in memory.

Syntax:

a == b

Example of the equality operator:

1  ==  1        // true
"2" ==  2        // true
3  == '3'       // true
0  == false     // true
0  == null      // false
0  == undefined   // false
null  == undefined   // true

If the operands are not equal, inequality operator (!=) returns true. When two operands are not of the same type, JavaScript tries to convert the operands to an appropriate type for the comparison.

If both operands are objects, JavaScript compares references that are not equal, when operands refer to different objects in memory.

Syntax:

a != b

Example of the inequality operator:

1 !=  2     // true
2 != "2"    // false
3 != '3'    // false
1 != true   // false
0 !=  false  // false

Boolean is the result

A comparison returns a value like all other operators. The value, in this case, is a boolean.

  • true is “yes”, “correct” or “the truth”;
  • false is “no”, “wrong” or “not the truth”.
alert( 3 > 1 );  // true (correct)
alert( 3 == 1 ); // false (wrong)
alert( 3 != 1 ); // true (correct)

A comparison result can be assigned to a variable, like any value:

let result = 7 > 4; // the result of the comparison
console.log ( result ); // true

String comparison

JavaScript uses “dictionary” or “lexicographical” order to see if one string is greater than another. In other words, strings are compared letter-by-letter.

Example of the string comparison:

alert( 'Z' > 'A' ); // true
alert( 'Want' > 'Walk' ); // true
alert( 'Too' > 'To' ); // true

In the examples the comparison 'Z' > 'A' gets to a result at the first step while the strings "Want" and "Walk" are compared character-by-character:

  • W is the same as W.
  • a is the same as a.
  • n is greater than l.

Comparison of different types

For comparing values of different types, JavaScript converts the values to numbers.

alert( '3' > 1 ); // true, string '3' becomes a number 3
alert( '02' == 2 ); // true, string '02' becomes a number 2

For boolean values, true becomes 1, false becomes 0.

alert( true == 1 ); // true
alert( false == 0 ); // true

Strict equality

A regular equality check == has a problem, as it can’t differentiate 0 from false:

alert( 0 == false ); // true

We meet the same thing with an empty string:

alert( '' == false ); // true

Comparison with null and undefined

Non-intuitive behavior is when null or undefined are compared to other values. For a strict equality check === These values are different, as each of them is a different type.

alert( null === undefined ); // false

For a non-strict check ==

These two are a “sweet couple”, it means that they equal each other.

alert( null == undefined ); // true

The Difference between == and ===

JavaScript has two visually similar, but very different ways to test equality:

== (Double equals operator): the equality or abstract comparison operator

=== (Triple equals operator): the identity or strict comparison operator

Here are the differences between == and ===:

  • before showing comparison == converts the variable values of the same type;
  • === does not do any type conversion and returns true only if both values and types are identical for the two variables being compared.
var foo = 13;
var bar = 13;
console.log(foo ==  bar); // true
console.log(foo === bar); // also true

For maths and other comparisons < > <= >=

Null/undefined are converted to numbers, here null becomes 0, undefined becomes NaN.

We present you some funny things that happen when we apply these rules. And also, how to not fall into a trap with them.

Strange result: null vs 0

Example of the compare null with a zero:

alert( null > 0 );  // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true

Mathematically, that’s strange. The last result says that "null is greater than or equal to zero", it means that in one of the comparisons above it must be true, but both of them are false.

The main reason is that an equality check == and comparisons > < >= <= work in a different way: comparisons convert null to a number, treating it as 0. That’s the reason why (3) null >= 0 is true and (1) null > 0 is false.

But on the other hand, the equality check == for undefined and null is defined without any conversions. They equal each other and don’t equal anything else, that’s why (2) null == 0 is false.

An incomparable undefined

We can’t compare the value undefined to other values:

alert( undefined > 0 ); // false (1)
alert( undefined < 0 ); // false (2)
alert( undefined == 0 ); // false (3)

The reasons why we get these results are:

  • Comparisons (1) and (2) return false, as undefined gets converted to NaN, it’s a special numeric value which returns false for all comparisons.
  • The equality check (3) returns false, as here undefined only equals null, undefined and no other value.



Do you find this helpful?

Related articles