JavaScript : Comparison Operators
Comparison Operators
Sometimes it is required to compare the value of one variable with other. The comparison operators take simple values (numbers or string) as arguments and evaluate either true or false. Here is a list of comparison operators.
| Operator | Comparisons |
Description |
|---|---|---|
| Equal (==) | x == y | Returns true if the operands are equal. |
| Strict equal (===) | x === y | Returns true if the operands are equal and of the same type. |
| Not equal (!=) | x != y | Returns true if the operands are not equal. |
| Strict not equal (!==) | x !== y | Returns true if the operands are not equal and/or not of the same type. |
| Greater than (>) | x>y | Returns true if the left operand is greater than the right operand. |
| Greater than or equal (>=) | x>=y | Returns true if the left operand is greater than or equal to the right operand. |
| Less than (<) | x<y | Returns true if the left operand is less than the right operand. |
| Less than or equal (<=) | x<=y | Returns true if the left operand is less than or equal to the right operand. |
Pictorial presentation of Equal (==) operator

Test Equal (==) operator
The following web document returns true though the type of x and y are not equal (first one is integer type and second one is character type) but their values are equal.
HTML Code
<!doctype html> <head> <meta charset="utf-8"> <title>JavaScript equal (==) operator example </title> <meta name="description" content="This document contains an example of JavaScript equal operator"/> </head> <body> <script src="javascript-equal-operator_example1.js"></script> </body> </html>
JS Code
var x = 300;
var y = "300";
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 300 and y = "300". Check whether x==y');
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode(x==y);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
View the example in the browser
Practice the example online
JavaScript this operator example
Pictorial presentation of Strict equal (===) operator

Test Strict equal (===) operator
The following web document returns false as strict equal operator will compare both value and type of x and y.
HTML Code
<!doctype html> <head> <meta charset="utf-8"> <title>JavaScript this operator example</title> <meta name="description" content="This document contains an example of JavaScript Strict equal (===) operator"/> </head> <body> <script src="javascript-strict-equal-operator-example1.js"></script> </body> </html>
JS Code
var x = 300;
var y = "300";
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 300 and y = "300" Whether x===y ?');
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode(x===y);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
View the example in the browser
Practice the example online
JavaScript this operator example
Pictorial presentation of Not equal(!=) operator

Test Not equal (!=) operator
The following web document returns false though the type of x and y are not equal (first one is integer type and second one is character type) but their values are equal.
HTML Code
<!doctype html> <head> <meta charset="utf-8"> <title>JavaScript not equal (!=) operator example with DOM</title> <meta name="description" content="This document contains an example of JavaScript not equal (!=) operator using DOM"/> </head> <body> <script src="javascript-not-equal-operator_example1.js"> </script> </body> </html>
JS Code
var x=150;
var y="150";
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 150 and y ="150"' + ', '+'Whether x != y ?');
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode(x !== y);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
Practice the example above online
JavaScript not equal (!=) operator example with DOM
Pictorial presentation of Strict not equal(!==) operator

Test Strict not equal (!==) operator
The following web document returns true though their values are equal but type of x and y are not equal (first one is integer type and second one is character type).
HTML Code
<!doctype html><head> <meta charset="utf-8"> <title>JavaScript strict not equal operator example</title> <meta name="description" content="This document contains an example of JavaScript strict not equal operator"/> </head> <body> <script src="javascript-strict-not-equal-operator_example1.js"></script> </body> </html>
JS Code
var x = 300;
var y = "300";
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 300 and y = "300"');
newParagraph.appendChild(newText); document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p"); var newText1 = document.createTextNode('Whether x !== y ? '+(x !== y));
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
View the example in the browser
Practice the example online
JavaScript strict not equal operator example
Pictorial presentation of Greater than(>) operator

Test Greater than(>) operator
The following web document returns true as the value of x is greater than y.
HTML Code
<!doctype html><head> <meta charset="utf-8"> <title>JavaScript greater than equal (>=) operator example </title> <meta name="description" content="This document contains an example of JavaScript greater than equal
(>=)operator"/> </head> <body> <script src="javascript-greater-than-equal-operator_example1.js"></script> </body> </html>
JS Code
var x = 300;
var y = 100;
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 300 and y = 100. Whether x > y ?');
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode(x > y);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
View the example in the browser
Practice the example online
JavaScript greater than(>) operator example.
Pictorial presentation of Greater than or equal (>=)

Test Greater than or equal (>=) operator
The following web document returns true as the value of x is equal to y.
HTML Code
<!doctype html> <head> <meta charset="utf-8"> <title>JavaScript greater than equal (>=) operator example </title> <meta name="description" content="This document contains an example of JavaScript greater than equal (>=) operator"/> </head> <body> <script src="javascript-greater-than-equal-operator_example1.js"> </script> </body> </html>
JS Code
var x = 300;
var y = 100;
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 300 and y = 100'+', '+'Whether x >= y ? '+ (x >= y));
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
View the example in the browser
Practice the example online
JavaScript greater than equal (>=) operator example
Pictorial presentation of Less than (<) operator

>Test Less than (<) operator
The following web document returns true as the value of x is less than y.
HTML Code
<!doctype html> <head> <meta charset="utf-8"> <title>JavaScript less than (<) operator example.</title> <meta name="description" content="This document contains an example of JavaScript less than (<) operator"/> </head> <body> <script src="javascript-less-than-operator_example1.js"></script> </body> </html>
JS Code
var x = 100;
var y = 300;
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 100 and y = 300. Whether x is less than y?');
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode(x<y);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
View the example in the browser
Practice the example online
JavaScript less than (<) operator example.
Pictorial presentation of Greater than or equal (>=)

Test Greater than or equal (>=) operator
The following web document returns true as the value of x is equal to y.
HTML Code
<!doctype html> <head> <meta charset="utf-8"> <title>JavaScript greater than equal (>=) operator example </title> <meta name="description" content="This document contains an example of JavaScript greater than equal (>=) operator"/> </head> <body> <script src="javascript-greater-than-equal-operator_example1.js"> </script> </body> </html>
JS Code
var x = 300;
var y = 100;
var newParagraph = document.createElement("p");
var newText = document.createTextNode('Value of x = 300 and y = 100'+', '+'Whether x >= y ? '+ (x >= y));
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
View the example in the browser
Practice the example above online
JavaScript greater than equal (>=) operator example
Pictorial presentation of Less than or equal (<=) operator

Test Less than or equal (<=) operator
The following web document returns false as the value of x is greater than y.
HTML Code
<!doctype html><head> <meta charset="utf-8"> <title>JavaScript less than equal(<=) operator example </title> <meta name="description" content="This document contains an example of JavaScript less than equal(<=) operator" /> </head> <body> <script src="javascript-less-than-equal-operator_example1.js"></script> </body> </html>
JS Code
var x = 300;
var y = 100;
var newParagraph = document.createElement("p");
var newText = document.createTextNode("Value of x = 300 and y = 100. Whether x <= y");
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode(x <= y);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
View the example in the browser

