Open In App
Related Articles

JavaScript Comparison Operators

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

JavaScript Comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values. Comparison operators are used in logical expressions to determine their equality or differences in variables or values. 

JavaScript Comparison Operators list: There are so many comparison operators as shown in the table with the description.

OPERATOR NAME

USAGE

OPERATION

Equality Operatora==bCompares the equality of two operators
Inequality Operatora!=bCompares inequality of two operators
Strict Equality Operatora===bCompares both value and type of the operand
Strict Inequality Operatora!==bCompares inequality with type
Greater than Operatora>bChecks if the left operator is greater than the right operator
Greater than or equal Operator a>=bChecks if the left operator is greater than or equal to the right operator
Less than Operatora<bChecks if the left operator is smaller than the right operator
Less than or equal Operatora<=bChecks if the left operator is smaller than or equal to the right operator

Equality (==): This operator is used to compare the equality of two operands. If equal then the condition is true otherwise false.

Example: Below example illustrates the (==) operator in JavaScript.

Javascript

// 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);

                    

Output:

true
true
true
true
false

Inequality(!=): This operator is used to compare the inequality of two operands. If equal then the condition is false otherwise true.

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

Javascript

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

                    

Output:

true
false
false
false
true

Strict equality(===): This operator is used to compare the equality of two operands with type. If both value and type are equal then the condition is true otherwise false.

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

Javascript

// Illustration of (===) operator
let val1 = 5;
let val2 = '5';
 
// Checking of operands
console.log(val1 === 6);
console.log(val2 === '5');       
console.log(val1 === val2);
 
// Check against null and boolean value
console.log(0 === false);  
console.log(0 === null);

                    

Output:

false
true
false
false
false

Strict inequality (!==): This operator is used to compare the inequality of two operands with type. If both value and type are not equal then the condition is true otherwise false.

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

Javascript

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

                    

Output:

true
false
true
true
true

Greater than (>): This operator is used to check whether the left-side value is greater than the right-side value. If the value is greater then the condition is true otherwise false.

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

Javascript

// 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);

                    

Output:

true
true
false
true

Greater than or equal (>=): This operator is used to check whether the left side operand is greater than or equal to the right side operand. If the value is greater than or equal then the condition is true otherwise false.

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

Javascript

// 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);

                    

Output:

true
true
true
false

Less than operator(<): This operator is used to check whether the left-side value is less than the right-side value. If yes then the condition is true otherwise false.

Example: Below examples illustrate the (<) operator in JavaScript.

Javascript

// 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);

                    

Output:

true
false
false
true

Less than or equal operator(<=): This operator is used to check whether the left side operand value is less than or equal to the right side operand value. If yes then the condition is true otherwise false.

Example: Below examples illustrate the (<=) operator in JavaScript.

Javascript

// 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);

                    

Output:

true
false
false
true

Supported Browsers: The browsers supported by all JavaScript Comparison operators are listed below:

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Microsoft Edge
  • Internet Explorer

We have a complete list of Javascript operators, to check those please go through this Javascript Operators Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Last Updated : 19 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads