Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Comparison or Relational Operators in JavaScript

Comparison Operators or Relational operators compares the values of the two operand. The comparison operators are less than <, less than or equal <=, greater than>, greater than equal >=, equal (== & ===) & not equal ( != & !==). All comparison operators return true or false.

Equality Operators == & ===

There are two operators for checking equality in JavaScript. One is (==) known as an equality operator or loose equality operator. The other one is (===) strict Equality operator.

Equality Operators in JavaScript

Not Equal Operators != & !==

!= operator checks the un equality of two operands. It is similar to == except it returns true if operands are not equal. The !== operator is similar to === except it returns true if operands are not equal

Not Equal Operators in JavaScript

Less than (<)

The Less than (<) comparison operator checks if the left operand is less than its right operand. If yes then it returns true otherwise returns false

Examples:

                            
let x=10
console.log(x < 20)    //true
console.log(x < 15)    //true
console.log(x < 10)    //false
console.log(x < 5)     //false
 
 
 
//String Examples
 
let y="Hello"
console.log(y < "I")     //true
console.log(y < "Hello") //false
console.log(y < "H")     //false
console.log(y < "J")     //true
 
                     
                            
                        

Greater than (>)

e greater than > operator checks if the value of the left operand is greater than the right operator. If yes then it returns true else false.

                            

let x=10
console.log(x > 20)    //false
console.log(x > 15)    //false
console.log(x > 10)    //false
console.log(x > 5)     //true
 
 
//String Examples
 
let y="Hello"
console.log(y > "I")     //false
console.log(y > "Hello") //false
console.log(y > "H")    //true
console.log(y > "J")    //false
                     
                            
                        

Less than or equal (<=)

The less than or equal (<=) operator returns true if the left operand is less than or equal to its right operand. else it evaluates to false.

        
let x=10
console.log(x <= 20)    //true
console.log(x <= 15)    //true
console.log(x <= 10)    //true
console.log(x <= 5)     //false
 
 
//String Examples
 
let y="Hello"
console.log(y <= "I")     //true
console.log(y <= "Hello") //true
console.log(y <= "H")     //false
console.log(y <= "J")     //true
 
 
        
    

Greater than or equal (>=)

The Greater than or equal (>=) relational operator returns true if its left operand is greater than or equal to its right operand else it returns false.

                            
let x=10
console.log(x >= 20)    //false
console.log(x >= 15)    //false
console.log(x >= 10)    //true
console.log(x >= 5)     //true
 
 
//String Examples
 
let y="Hello"
console.log(y >= "I")     //false
console.log(y >= "Hello") //true
console.log(y >= "H")     //true
console.log(y >= "J")     //false
 
                     
                            
                        

How Comparison works

The comparison is done only using the number comparison or string comparison. If the operands are of different types, then they are either converted to numbers or strings before they are compared

TFor Example

If both operands are numbers, then use the number comparison.

                            
//number comparison
console.log(10 > 5) //true
 
                     
                            
                        

if both operands are string, then uses the string comparison

                            
//string comparison
console.log("10" > "5") //false
                     
                            
                        

If one of the operands is a number, the other operand is converted to a number and then uses the number comparison. If the other operand does not convert to a number then the result is always false

                            
// number comparison.  The non number operand is converted to number
 
console.log(10 > "5")   //true     
 
console.log(2 > true)   //true     // true is 1
console.log(1 > false)  //true     // false is 0
console.log(1 > "")     //true     // "" is 0
console.log(1 > " ")    //true     // " " is 0
console.log(1 > null)   //true     // null 0   
 
//Compiler warns here because of the type change.
                     
                            
                        
                            
// number comparison.  The non number does not convert to Number. Result is always false
 
 
 console.log(10 > "a")  //false
 console.log(10 > NaN)  //false
 console.log(10 > undefined)  //false
  
                     
                            
                        

If the operands are neither string nor number, and if they do convert to number or string, then the comparison operator converts them and compares them. The Example is a Date that converts to a number.

                            
var date1 = new Date() 
var date2 = new Date() 
 
console.log(date1 > date2)  //false
console.log(date1 < date2)  //false
 
console.log(date1 >= date2) //true
console.log(date1 <= date2) //true
 
 
//Only Equality does not work. Becuase they are objects
console.log(date1 == date2) //false
console.log(date1 === date2) //false
 
  
                     
                            
                        
                            
var date1 = new Date('2020-11-01');
var date2 = new Date('2020-11-20');
 
console.log(date1 > date2)  //false
console.log(date1 < date2)  //true
 
console.log(date1 >= date2) //false
console.log(date1 <= date2) //true
 
//Only Equality does not work. Because they are objects
console.log(date1 == date2) //false
console.log(date1 === date2) //false
 
  
                     
                            
                        

Note that using Equality Operators == & === does not work on dates. Because the Date is an object and to be considered equal, they must point to the same object.

Comparison involving any other types will always result in false.

Comparison Operators and Strings

The strings comparison uses the dictionary or lexicographical order. And the comparison is done character-by-character basis, using the Unicode values of the characters.

A character-by-character comparison starts with comparing the first character of both operands. If these are greater or less, then the comparison ends and the result is returned.

If the characters are equal, then the comparison moves to the next character. And the process continues until it reaches the end.

In the end, if both operands are finished, then the strings are Equal. else the operand with a longer length is considered greater.

The string comparison is case-sensitive. All capital letters are “less than” all lowercase letters. For a case insensitive comparison, you need to convert the string either to upper case or lower case.

Comparison Operators and Date

As you can see, the comparison operators on date work correctly. But Equality operators fail. An interesting thing to note here is that the >= & <= operators also check for Equality.

                            
var date1 = new Date('2020-11-01');
var date2 = new Date('2020-11-01');
 
//Correct
console.log(date1 >  date2) //false
console.log(date1 >= date2) //true
console.log(date1 <= date2) //true
console.log(date1 <  date2) //false
 
//Wrong
console.log(date1 ==  date2) //false
console.log(date1 === date2) //false
 
  
                     
                            
                        

The difference is due to the fact how javascript handles these comparisons. The Equality Operators in JavaScript check if the objects are of the same instance

                            
//date1 & date2 has same value, but are different objects
 
var date1 = new Date('2020-11-01');
var date2 = new Date('2020-11-01');
 
console.log(date1 == date2) //false    
console.log(date1 === date2) //false
 
 
 
//date3 & date1 are now same objects
var date3 = date1   
 
console.log(date1 == date3) //true
console.log(date1 === date3) //true
 
 
  
                     
                            
                        

The comparison operators do not check for equality, when we use >= & <= operators.

The greater than or equal (date1 >= date2) internally uses the not greater than , (!(date1 < date2)) Hence avoiding the use of Equality Operators.

                            
var date1 = new Date('2020-11-01');
var date2 = new Date('2020-11-01');
 
console.log(date1 >= date2)  //true
 
//The above is converted to the following internally
console.log(!(date1 < date2)) //true
 
 
  
                     
                            
                        

Similarly, the less than or equal uses the not less than internally as shown below.

                            
var date1 = new Date('2020-11-01');
var date2 = new Date('2020-11-01');
 
console.log(date1 <= date2)  //true
 
//The above is converted to the following internally
console.log(!(date1 > date2)) //true