...

Web Technologies - JAVASCRIPT

Back to Course

Lesson Description


Lession - #229 Javascript Comparisons


JavaScript Comparison Operators

In this tutorial, we'll learn about several comparison operators and how to use them in Javascript. The comparison operators are mostly used to execute logical operations that determine if two values are equal or different. On operands, operators are accustomed to executing precise mathematical and logical computations. JavaScript, like C, C++, Java, Python, and other languages, allows comparison operations. In logical expressions, comparison operators are used to determine if variables or values are equal or different. The comparison operators are demonstrated in the example below.
<script>
    function gfg(>
{ let val1 = 5; // Equality Operators document.write(val1 == 5>
; document.write("<br>">
; // Relational Operators document.write(val1 > 0>
; } gfg(>
; </script>


Output:
true
true


JavaScript supports a number of different comparison operators: Relational Operators Equality Operators We'll go over both operators one by one as we go through the examples. Operators for Equality: Equality (==>
: This operator is used to compare two operands' equality. If the conditions are equal, the condition is true; otherwise, it is false.
Syntax:
x == y 
Example 1: Below example illustrate the (==>
operator in JavaScript.

<script>
 
    // Illustration of (==>
operator let val1 = 5; let val2 = '5'; // Checking of operands console.log(val1 == 5>
; console.log(val2 == 5>
; console.log(val1 == val1>
; // Check against null and boolean value console.log(0 == false>
; console.log(0 == null>
; </script>
Output:
> true
> true
> true
> true
> false

Try it here

Example 2: This example describes the equality operator to compare the 2 object’s values.
<script>
 
    // Illustration of (==>
operator let obj1 = {'val1': 'value'}; let obj2 = {'val2': 'value'}; // Checking of operands console.log(obj1.val1 == 'value'>
; console.log(obj1 == obj2>
; console.log(obj1.val1 == obj2.val2>
; // Check against undefined console.log(0 == undefined>
; console.log(null == undefined>
; </script>
Output:
> true
> false
> true
> false
> true

Try it here


Inequality(!=>
: This operator is used to compare two operands' inequality. If the conditions are equal, the condition is false; otherwise, it is true. Syntax:
x!= y
Example 1:
The (!=>
operator in JavaScript is demonstrated in the examples below.


<script>
 
    // Illustration of (!=>
operator let val1 = 5; let val2 = '5'; // Checking of operands console.log(val1 != 6>
; console.log(val2 != '5'>
; console.log(val1 != val1>
; // Check against null and boolean value console.log(0 != false>
; console.log(0 != null>
; </script>
Output:

> true
> false
> false
> false
> true

Try it here

Example 2: This example describes the inequality operator to compare the 2 values.

<script>
 
    // Illustration of (!=>
operator let obj1 = {'val1': 'value'}; let obj2 = {'val2': 'value'}; // Checking of operands console.log(obj1.val1 != 'value'>
; console.log(obj1 != obj2>
; console.log(obj1.val1 != obj2.val2>
; // Check against undefined console.log(0 != undefined>
; console.log(null != undefined>
; </script>
Output:
> false
> true
> false
> true
> false

Try it here

Strict equality(===>
: This operator compares the equality of two operands that have the same type. The condition is true if both value and type are equal; otherwise, it is false. Syntax:
x === y


Example 1: Below examples illustrate the (===>
operator in JavaScript.

<script>
 
    // Illustration of (===>
operator let val1 = 5; let val2 = '5'; // Checking of operands console.log(val1 === 6>
; console.log(val2 === '5'>
; console.log(val1 === val1>
; // Check against null and boolean value console.log(0 === false>
; console.log(0 === null>
; </script>
Output:
> false
> true
> true
> false
> false

Try it here

Example 2: This example describes the strict equality operator to compare the 2 values.


<script>
 
    // Illustration of (===>
operator let obj1 = {'val1': 'value'}; let obj2 = {'val2': 'value'}; // Checking of operands console.log(obj1.val1 === 'value'>
; console.log(obj1 === obj2>
; console.log(obj1.val1 === obj2.val2>
; // Check against undefined console.log(0 === undefined>
; console.log(null === undefined>
; </script>
Output:
> true
> false
> true
> false
> false

Try it here

Strict inequality(!==>
: This operator compares the inequality of two operands that have the same type. The condition is true if both value and type are not equal; otherwise, it is false. Syntax:
x !== y

Example 1: Below examples illustrate the (!==>
operator in JavaScript.

<script>
 // Illustration of (!==>
operator let val1 = 5; let val2 = '5'; // Checking of operands console.log(val1 !== 6>
; console.log(val2 !== '5'>
; console.log(val1 !== val1>
; // Check against null and boolean value console.log(0 !== false>
; console.log(0 !== null>
; </script>
Output:
> true
> false
> false
> true
> true

Try it here

Example 2: This example describes the strict inequality operator to compare the 2 values.

<script>
 
    // Illustration of (!==>
operator let obj1 = {'val1': 'value'}; let obj2 = {'val2': 'value'}; // Checking of operands console.log(obj1.val1 !== 'value'>
; console.log(obj1 !== obj2>
; console.log(obj1.val1 !== obj2.val2>
; // Check against undefined console.log(0 !== undefined>
; console.log(null !== undefined>
; </script>
Output:
> false
> true
> false
> true
> true

Try it here

Operators for Relationships:
The greater than operator (>>
is used to see if the left-hand value is greater than the right-hand value. The condition is true if the value is greater; else, it is false.
Syntax:
x > y 
Example 1: The (>>
operator in JavaScript is demonstrated in the examples below.

<script>
 
    // Illustration of (>>
operator let val1 = 5; let val2 = "5"; // Checking of operands console.log(val1 > 0>
; console.log(val2 > "10">
; console.log(val1 > "10">
; console.log(val2 > 0>
; </script>
Output:
> true
> true
> false
> true

Try it here

Example 2: This example describes the greater than operator to compare the 2 values.

<script>
 
    // Illustration of (>>
operator let obj1 = {'val1': 1}; let obj2 = {'val2': 3}; // Checking of operands console.log(obj1.val1 > 0>
; console.log(obj1 > obj2>
; console.log(obj1.val1 > obj2.val2>
; console.log(obj2 > obj1>
; console.log(obj2.val2 > obj1.val1>
; </script>
Output:
> true
> false
> false
> false
> true

Try it here

The greater than or equal operator (>=>
is used to determine if the left operand is greater than or equal to the right operand. The condition is true if the value is more than or equal to the value, else it is false.
Syntax:
x >= y
 
Example 1: The (>=>
operator in JavaScript is demonstrated in the examples below.

<script>
 
 // Illustration of (>=>
operator let val1 = 5; let val2 = "5"; // Checking of operands console.log(val1 >= 5>
; console.log(val2 >= "15">
; console.log(val1 >= "5">
; console.log(val2 >= 15>
; </script>
Output:
> true
> true
> true
> false

Try it here

Example 2: This example describes the greater than or equal operator to compare the 2 values.

<script>
 
    // Illustration of (>=>
operator let obj1 = {'val1': 1}; let obj2 = {'val2': 3}; // Checking of operands console.log(obj1.val1 >= 0>
; console.log(obj1 >= obj2>
; console.log(obj1.val1 >= obj2.val2>
; console.log(obj2 >= obj1>
; console.log(obj2.val2 >= obj1.val1>
; </script>
Output:
> true
> true
> false
> true
> true

Try it here

The less than operator(>
is used to determine whether the left-hand value is less than the right-hand value. If the answer is affirmative, the condition is true; otherwise, it is false. Syntax:
x < y
Example 1: Below examples illustrate the (<>
operator in JavaScript.

<script>
 
 // Illustration of (<>
operator let val1 = 5; let val2 = "5"; // Checking of operands console.log(val1 < 15>
; console.log(val2 < "0">
; console.log(val1 < "0">
; console.log(val2 < 15>
; </script>
Output:
> true
> false
> false
> true

Try it here

Example 2: This example describes the Less than operator to compare the 2 values.

<script>
 
    // Illustration of (<>
operator let obj1 = {'val1': 1}; let obj2 = {'val2': 3}; // Checking of operands console.log(obj1.val1 < 10>
; console.log(obj1 < obj2>
; console.log(obj1.val1 < obj2.val2>
; console.log(obj2 < obj1>
; console.log(obj2.val2 < obj1.val1>
; </script>
Output:
> true
> false
> true
> false
> false

Try it here


Strict equality(===>
: This operator compares the equality of two operands that have the same type. The condition is true if both value and type are equal; otherwise, it is false.

Syntax:
x <= y
Example 1: Below examples illustrate the (<=>
operator in JavaScript.

<script>
 
    // Illustration of (<=>
operator let val1 = 5; let val2 = "5"; // Checking of operands console.log(val1 <= 15>
; console.log(val2 <= "0">
; console.log(val1 <= "0">
; console.log(val2 <= 15>
; </script>
Output:
> true
> false
> false
> true

Try it here

Example 2: This example describes the Less than or equal to an operator to compare the 2 values.

<script>
 
    // Illustration of (<=>
operator let obj1 = {'val1': 1}; let obj2 = {'val2': 3}; // Checking of operands console.log(obj1.val1 <= 10>
; console.log(obj1 <= obj2>
; console.log(obj1.val1 <= obj2.val2>
; console.log(obj2 <= obj1>
; console.log(obj2.val2 <= obj1.val1>
; </script>
Output:
> true
> true
> true
> true
> false

Try it here