Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Comparison Operators in JavaScript, Relational, Example

Comparison (Relational) operators in JavaScript are those operators that are used for comparing between two numeric values, strings, objects, or two quantities.

The comparison operators determine the relationship between them by comparing operands (or values).

For example, to know which is a greater number, comparing the age of two persons, comparing the price of two items, etc.

These kinds of comparisons can be performed with the help of comparison operators. The result of all comparison operators is a boolean value, either true or false.

Relational operators in JavaScript are mainly used to create conditions in decision-making statements or loops in order to perform actions only when a certain condition is met.

Types of Comparison Operators in JavaScript


JavaScript supports eight types of relational or comparison operators. They are listed in the below table:

Comparison Operator Description
== (Equal) This operator evaluates that the value of left and right operands are equal or not. If values on both sides of the operator are equal to each other, the equal operator (==) returns true.
!= (Not Equal) This operator evaluates that the left operand is not equal to the right side. If the values on both sides of operator are not equal to each other, the not equal operator returns true.
> (Greater than) This operator evaluates that the value of the left operand is greater than the value of the right operand. If the value on left side of operator is greater than the value on right side, the greater than (>) operator returns true.
This operator checks whether the value of the left operand is less than the value of the right operand. If the value on the left side of operator is less than the value on the right side, the result becomes true.
>= (Greater than or Equal to) This operator tests that the value of the left operand is greater than or equal to the value of the right operand. If the value on the left side of the operator is greater than or equal to the value on the right side, then the operator returns true.
This operator evaluates that the value of the left operand is less than or equal to the value of the right operand. If it is the case, the operator returns true.
=== (Strictly Equal or Identical) This operator strictly checks whether the left and right values are equal and of the same data type. If the values on both sides are equal and of the same data type, the strict is equal to (===) operator returns true.
!== (Strictly not Equal or not Identical) This operator strictly checks whether the left and right values are not equal and not of the same data type. If the values on both sides are not equal or not of the same data type, the strict is not equal to (!==) operator returns true.

The above eight operators provide the foundation for comparing data in JavaScript. Each operator is always a combination of two constants, variables, expressions, or a mix of these.


Let’s take an example program based on the concepts of all comparison operators.

Program code 1:


    Comparison Operators Example Program
Output:
      x = 10 y = 4
      (x > y): true
      (x = y): true
      (x 

Let’s create a JavaScript program where we will compare a number to a string. In such a case, the string is automatically converted to a number.

Program code 2:


     Relational Operators Example Program
Output:
    x = 5 y = 5
   (x > y): false
   (x = y): true
   (x 

In the preceding program, 5 ==”5″ is true because “5” string is automatically converted into number. Whereas, 5 === “5” is false because both operands are not of the same type.


Let’s create a JavaScript program in which we will compare a boolean with a number. When one of the operands is boolean, it is automatically converted to 1 for true and 0 for false.

Program code 3:


 
Output:
    x = 0 y = false
   (x > y): false
   (x = y): true
   (x 

Note: The strict equality operator === and !== are used when the operands are of the same type.


Try It Yourself

Program code 7:


 

Comparison Operators and Data Conversion


We can also have two operands of different data types within a single comparison expression. In this case, JavaScript always tries to convert each operand into a number before running the comparison. Let’s understand different cases with examples.

Case 1: If one operand is string and another is number, JavaScript tries to convert the string into a number. For example:

Program code :


Output:
     Result: true

In this program, string “12” gets converted to a number 12. Therefore, the comparison num1 == num2 return true.

Case 2: If the string cannot be converted into a number, comparison always returns false. For example:

Program code :


Output:
     Result: false

Case 3: If one operand is boolean and another is a number in an expression, JavaScript converts the boolean to a number, as follows:

  • true: This boolean value gets converted to 1.
  • false: This boolean value gets converted to 0.

For example, in the below program, the boolean true gets converted to the number 1. Therefore, the comparison num1 == num2 returns true.

Program code :


Output:
     Result: true

Case 4: If one value is boolean and another is a string, JavaScript attempts to convert boolean into a number and converts a string into a number. For example:

Program code :


Output:
    Result: true

In the preceding program, the boolean false gets converted to a number 0 and the string “0” also gets converted to a number 0. Therefore, the comparison num1 == num2 returns true.

Strict Equality (Identity) Operator in JavaScript


The strict equality operator (===) was introduced in JavaScript 1.5 version. It returns true if two operands must be the same data type and must be equal without any data conversion.

The syntax of strict equality operator is as follows:

A === B // Here, A and B are two operands.

For example, a statement 5 === “5” will return false because the value on the left is number and the value on the right is string. Here, values are of different data type.

If we compare two strings using strict equality operator, the strict equality operator will return true only if they have the same sequence of character and same length. For example, the statement “ABc” === “ABc” will return true.

If we compare two boolean values, they will strictly equal if either both of them are true or both of them are false. Let’s take an example program based on the strict equality operator.

Program code :


Output:
     true
     false
     false

Try It Yourself

Program code :


Program code :


Non-Identity/Strict Inequality Operator in JavaScript


The non-identity operator (!==) was added in JavaScript 1.5 version. It performs the opposite function to the strict equality operator. It returns true in either of the following cases without any data conversion:

  • The two operands are not equal.
  • The two operands are not of the same data type.

The syntax of non-identity or strict inequality operator is as follows:

A !== B

Consider the following example program of non-identity operator.

Program code :


Output:
      true

In this program, the operands p and q are not of the same data type. Therefore, it will return true.

Try It Yourself

Program code :


Program code :


 

String Comparison in JavaScript


If both operands are strings in an expression, they are compared as strings. JavaScript interpreter compares the ASCII codes for each character in both strings, such as the first character of each string, second character of each string, and so on.

Uppercase letter A is represented in ASCII by the number 65, B by 66, C by 67, and so on. Whereas, lowercase letter a is represented in ASCII code by 97, b by 98, c by 99, and so on.

Keep in mind that string comparison is performed on a strict character by character basis, using the numerical value of each letter of ASCII code.

Let’s take some important example programs based on string comparison in JavaScript.

Program code :


Output:
       false

Program code :


Output:
    true

Program code :


Output:
     true

Try It Yourself

Program code :



In this tutorial, you learned eight types of comparison or relational operators in JavaScript and their functions with various example programs. Hope that you will have understood all basic points related to relational operators and practiced all example programs.

In the next tutorial, we will learn logical operators in JavaScript with various example programs.
Thanks for reading!!!

The post Comparison Operators in JavaScript, Relational, Example appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Comparison Operators in JavaScript, Relational, Example

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×