Learn JavaScript

JavaScript Comparison Operators

JavaScript Comparison Operators are used to manipulate the logical statement, in order to determine whether these statements are equal or not. On the basis of that manipulation the comparison operators returns two types of truth values; true or false. It includes the operators like {==, ===, !=, !==, >, <, >=, <=}.

In order to understand the working of comparison operators, let us take a be a variable with initial value 3.

OperatorsDescriptionExpressionTruth Values
==equal toa==3
a==5
a==”3″
true
false
true
===equal value and typea===3
a===”3″
true
false
!=not equal toa!=3
a!=4
false
true
!==neither value nor type is equala!==3
a!==”3″
a!==4
false
true
true
<less thana<3
a<6
false
true
>greater thana>3
a>8
a>2
false
false
true
<=less than or equal toa<=3
a<=5
a<=2
true
true
false
>=greater than or equal toa>=3
a>=2
a>=8
true
true
false

Note: Comparison Operators are generally used in conditional statements to compare the values, depending on such comparison some actions are taken.


Example on JavaScript Comparison (Relational) operators

<html>
<body>
<script>
function jco(){
var a = 3;
// JavaScript == Operators
document.write(a==3);
document.write("<br>");
document.write(a==5);
document.write("<br>");
document.write(a=="3");
document.write("<p>");
// JavaScript === Operators
document.write(a===3);
document.write("<br>");
document.write(a==="3");
document.write("<p>");
// JavaScript != Operators
document.write(a!=3);
document.write("<br>");
document.write(a!=4);
document.write("<p>");
// JavaScript !== Operators
document.write(a!==3);
document.write("<br>");
document.write(a!=="3");
document.write("<br>");
document.write(a!==4);
document.write("<p>");
// JavaScript < Operators
document.write(a<3);
document.write("<br>");
document.write(a<6);
document.write("<p>");
// JavaScript > Operators
document.write(a>3);
document.write("<br>");
document.write(a>8);
document.write("<br>");
document.write(a>2);
document.write("<p>");
// JavaScript <= Operators
document.write(a<=3);
document.write("<br>");
document.write(a<=5);
document.write("<br>");
document.write(a<=2);
document.write("<p>");
// JavaScript >= Operators
document.write(a>=3);
document.write("<br>");
document.write(a>=2);
document.write("<br>");
document.write(a>=8);
}
jco();
</script>
</body>
</html>

Output

true
false
true

true
false

false
true

false
true
true

false
true

false
false
true

true
true
false

true
true
false


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.