Comparison Operators

Comparison Operators

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:

console.log(7 > 4); // true

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

Syntax:

a < b

Example of the less-than operator:

console.log(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:

console.log(7 >= 4); // true
console.log(4 >= 4); // true

Less than or equal operator (a <= b)< strong> 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:


console.log(4 <= 7); // true
console.log(4 <= 4); // true

The equality operator (a == b) converts the operands if they are not of the same type, then applies strict comparison. If 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:

console.log(1 == 1); // true
console.log("2" == 2); // true
console.log(3 == '3'); // true
console.log(0 == false); // true
console.log(0 == null); // false
console.log(0 == undefined); // false
console.log(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:


console.log(1 !=  2) ;    // true
console.log(2 != "2");    // false
console.log(3 != '3' );   // false
console.log(1 != true);   // false
console.log(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”.
console.log( 3 > 1 );  // true (correct)
console.log( 3 == 1 ); // false (wrong)
console.log( 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 a “dictionary” or “lexicographical” order to see if one string is greater than another. In other words, strings are compared to letter-by-letter.

Example of the string comparison:

console.log( 'Z' > 'A' ); // true
console.log( 'Want' > 'Walk' ); // true
console.log( '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.

console.log( '3' > 1 ); // true, string '3' becomes a number 3

console.log( '02' == 2 ); // true, string '02' becomes a number 2

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

console.log( true == 1 ); // true
console.log( false == 0 ); // true

Strict equality

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

console.log( 0 == false ); // true

We meet the same thing with an empty string:

console.log( '' == 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.

console.log( null === undefined ); // false

For a non-strict check ==

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

console.log( 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 of conversion and returns true only if both values and types are identical for the two variables being compared.
var val1 = 13;
var val2 = 13;
console.log(val1 == val2); // true
console.log(val1 === val2); // also true

For maths and other comparisons < > <=>=

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

We present to you some fun 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:

console.log( null > 0 );  // (1) false
console.log( null == 0 ); // (2) false
console.log( 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 > < >= <= < strong>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:

console.log( undefined > 0 ); // false (1)

console.log( undefined < 0 ); // false (2)

console.log( 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.
Reactions

Post a Comment

0 Comments

close