Relational Operators – Programming Fundamentals in Javascript

Relational operators are also known as comparison operators.

They are used to find the relationship between two values or compare the relationship between two values; on comparison, they yield the result true or false.

Less than (<) The < operator evaluates to true if its first operand is less than its second operand; otherwise it evaluates to false.

Greater than (>) The > operator evaluates to true if its first operand is greater than its second operand; otherwise, it evaluates to false. 

Less than or equal (<=) The <= operator evaluates to true if its first operand is less than or equal to its second operand; otherwise, it evaluates to false.

Greater than or equal (>=) The >= operator evaluates to true if its first operand is greater than or equal to its second operand; otherwise, it evaluates to false.

Example 1

When both operands are numeric, they are compared normally:

        
a= 5 < 7       			
b= 8 <= 8       			
c= 9 >= 1     		 	
d= true < false		

console.log(a);     // Output : true
console.log(b);     // Output : true
console.log(c);     // Output : true
console.log(d);     // Output : false
        
    

Example 2

 When both operands are strings, they are compared lexicographically (accordingto alphabetical order)

        
a= 'x' < 'y'        // Output : true

b= '5' < '9'        // Output : true

c= '10' > '15'     	// Output : false        
    

Example 3

When one operand is a string and the other is a number, the string is converted to a number before comparison:

        
a= '1' < 2         // Output : true

b= '3' > 2         // Output : true

c= '10' > '15'     // Output : false        
    

Example 4

When the string is non-numeric, numeric conversion returns NaN (not-a-number). Comparing with NaN always returns false:

        
a= 1 < 'xyz'  	 // Output : false

b= 1 > 'xyz'     // Output : false        
    
Posted in

Ankit Patel

Leave a Comment

Your email address will not be published.

Scroll to Top