What Are JavaScript Comparison Operators?

 What Are JavaScript Comparison Operators?

What Are JavaScript Comparison Operators?

Comparison Operator

Comparison Operator is also called a relational operator in computer science. This is a programming language construct or operator. That defines and tests some kind of relation between two objects. These comprise numerical equality e.g., 5 = 5 and inequalities e.g., 4 ≥ 3. Usually, these operators evaluate to true or false, if the conditional relationship between the two operands holds or not. Comparison operators may be understood as distinct cases of logical bases.

Comparison operators in JavaScript are written like this:

  • Greater or less than: a > b, a < b.
  • Greater or less than or equals: a >= b, a <= b.
  • Equals: a == b, (Double equality sign) == means the equality test, though a single one a = b means an assignment.
  • Not equals, a! = b.

Triple Equality sign (===)

This is a type of comparison operator, precisely an equality operator. We use it to compare two things to understand if they’re equal. We may use the equality operator to relate a variable with a string, a variable with a number, a variable with a math expression, or a variable with a variable. We can use it to compare various combinations. Below are all legal first lines in if statements:

if (fullName === “Mansoor” + ” ” + “Ahmed”) {

if (fullName === firstName + ” ” + “Mansoor”) {

if (fullName === firstName + ” ” + “Mansoor”) {

if (fullName === firstName + ” ” + lastName) {

if (totalCost === 81.50 + 135) {

if (totalCost === materialsCost + 135) {

if (totalCost === materialsCost + laborCost) { if (x + y === a – b) {

The equality operator is case-sensitive. “Rose” does not equal “rose”, when we’re comparing strings. Another comparison operator, !==, is the opposite of ===.It means is not equal to.

1 if (myTicketNumber !== 487208) {

2 alert(“Better luck next time.”);

3 }

Alike ===, the not-equal operator can be used to compare numbers, strings, variables, math expressions, and combinations. Using the not-equal operator are case-sensitive, Like ===, string comparisons. It’s true that “Rose”! == “rose”. Following are 4 more comparison operators, typically used to compare numbers.

> is greater than

< is less than

>= is greater than or equal to

<= is less than or equal to

Result in Boolean

Boolean value is being returned by all comparison operators:

  • True: means “yes”, “correct” or “the truth”.
  • False: means “no”, “wrong” or “not the truth”.

False: means “no”, “wrong” or “not the truth

For example:

alert ( 2 > 1 ); // true (correct)

alert ( 2 == 1); // false (wrong)

alert( 2 != 1 ); // true (correct)

The result of a comparison can be assigned to a variable, same like any value:

let result = 6 > 5; // allocate the result of the comparison

alert ( result); // true

String comparison

JavaScript uses the supposed “dictionary” or “lexicographical” order to understand whether a string is greater than another. Strings are likened letter-by-letter.

For example: The algorithm to compare two strings is simple:

  1. Relate the first character of both strings.
  2. If the first character from the first string is greater or less than other string’s, then the first string is greater or less than the second. We’re completed.
  3. Then, compare the second characters the same way, if both strings’ first characters are the same.
  4. Reiterate until the end of any string.
  5. They are equal, if both strings end at the same length. Else, the longer string is greater.

Comparison of different types

JavaScript changes the values to numbers when linking values of different types.

For example:

alert( ‘2’ > 1 ); // true, string ‘2’ becomes a number 2

alert( ’01’ == 1 ); // true, string ’01’ becomes a number 1

True becomes 1 and false becomes 0 for boolean values. For example:

alert( true == 1 ); // true

alert ( false == 0); // true

Strict equality

A regular equality check == may not differentiate 0 from false:

alert ( 0 == false); // true

The similar thing occurs with an empty string:

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

This occurs due to operands of diverse types are converted to numbers by the equality operator ==. Just like false, an empty string, becomes a zero. What to prepare if we’d like to distinguish 0 from false? A firm equality operator === checks the equality without type conversion. We can say in other words, if a and b are of different types, then a === b directly returns false without an attempt to convert them. Let’s attempt it:

Alert (0 === false); // false, due to the types are different. There is also a “strict non-equality” operator! == analogous to !=. The firm equality operator is a bit longer to write. It makes clear what’s going on and leaves less room for errors.

Comparison with null and undefined

When null or undefined are compared to other values there’s a non-intuitive behavior.

Due to each of them for a strict equality check === these values are different is a different type. Alert (null === undefined); // false

If a non-strict check == there’s a special rule. These two are a “sweet couple”. They equal each other. They have not any other value. alert (null == undefined); // true. Null becomes 0, while undefined becomes NaN for maths and other comparisons < > <= >= null or undefined are converted to numbers. When we apply these rules, let’s see what happen. The others that is most important, how to not fall into a trap with them.

Strange result: null vs 0

Let’s compare null with a zero:

alert( null > 0 ); // (1) false

alert( null == 0 ); // (2) false

alert (null >= 0 ); // (3) true

The last result shapes that “null is greater than or equal to zero”. Therefore, in one of the comparisons above it must be true, but they are both false. The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, treating it as 0. That’s why (3) null >= 0 is true and (1) null > 0 is false.

Leave a Comment