Comparison Operators in JavaScript

Comparison operators in JavaScript, are used to compare between two variables or values. For example:

HTML with JavaScript Code
<!DOCTYPE html>
   <p id="xyz"></p>

      let x = 20;
      let y = 20;
      if(x==y) {
         document.getElementById("xyz").innerHTML = "The value of 'x' is equal to the value of 'y'";
      } else {
         document.getElementById("xyz").innerHTML = "The value of 'x' is not equal to the value of 'y'";

Note - Comparison operators in JavaScript, returns boolean value (true/false). Therefore x==y or 20==20 returns true, because both numbers are obviously equals to each other.

List of All Comparison Operators in JavaScript

Operator Name
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
< less than
> greater than
<= less than or equal to
>= greater than or equal to

JavaScript Comparison Operators Example

HTML with JavaScript Code
<!DOCTYPE html>
   <p id="para1"></p>
   <p id="para2"></p>
   <p id="para3"></p>
   <p id="para4"></p>
   <p id="para5"></p>
   <p id="para6"></p>
   <p id="para7"></p>
   <p id="para8"></p>
   <p id="para9"></p>
   <p id="para10"></p>
   <p id="para11"></p>
   <p id="para12"></p>
   <p id="para13"></p>
   <p id="para14"></p>
   <p id="para15"></p>
   <p id="para16"></p>
   <p id="para17"></p>
   <p id="para18"></p>
      let a = 10, b = 10, c = 20, d = "10";
      document.getElementById("para1").innerHTML = a==b;
      document.getElementById("para2").innerHTML = a==c;
      document.getElementById("para3").innerHTML = a===b;
      document.getElementById("para4").innerHTML = a===c;
      document.getElementById("para5").innerHTML = a===d;

      document.getElementById("para6").innerHTML = a!=b;
      document.getElementById("para7").innerHTML = a!=c;

      document.getElementById("para8").innerHTML = a!==b;
      document.getElementById("para9").innerHTML = a!==c;
      document.getElementById("para10").innerHTML = a!==d;
      document.getElementById("para11").innerHTML = a>b;
      document.getElementById("para12").innerHTML = a>c;

      document.getElementById("para13").innerHTML = a<b;
      document.getElementById("para14").innerHTML = a<c;

      document.getElementById("para15").innerHTML = a>=b;
      document.getElementById("para16").innerHTML = a>=c;

      document.getElementById("para17").innerHTML = a<=b;
      document.getElementById("para18").innerHTML = a<=c;

JavaScript Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube