Difference Between =, == And === In JavaScript

In this article, I am going to demonstrate the difference between =, == and === in javascript. I have chosen a small example to demonstrate the comparision of == and ===. 

  • = is used for assigning values to a variable in JavaScript.
  • == is used for comparison between two variables irrespective of the datatype of variable.
  • === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.

Let's take the example of each one by one.

Example of =
  1. var number = 100;  // Here number variable assigned using =

Example of == 

  1. if (number == 100)  // Here Comparision between two values using ==. It will compare irrespective of datatype of variable
  2.    alert("Both are equal");    
  3. else    
  4.    alert("Both are not equal");   

Example of ===

  1. if (number === 100)  // Here Comparision between two values using ===. It will compare strict check means it will check datatype as well.
  2.    alert("Both are equal");      
  3. else      
  4.    alert("Both are not equal");     

Let's take an example below to understand how == and === do a comparison of two variable values. 

  1. <h2>Difference between =, == and === in Javascript</h2>  
  2. <script type="text/javascript">  
  3.     function Comparision() {  
  4.         var number = 100;  // Here number variable assigned using =  
  5.         debugger;  
  6.   
  7.         if (number == 100)  // Here Comparision between two values using ==. This will not check datatype irrespective of datatype it will                                  do comparision  
  8.             $("#lblMessage").text("Both are equal");  
  9.         else  
  10.             $("#lblMessage").text("Both are not equal");  
  11.   
  12.       if(number == "100")  //Here Comparision between two values using ==. This will not check datatype irrespective of datatype it will                              do comparision  
  13.             $("#lblMessage1").text("Both are equal");  
  14.         else  
  15.            $("#lblMessage1").text("Both are not equal");  
  16.     }  
  17. </script>  
Difference between =, == and === in JavaScript 

In the above example, both the comparisons return a true value irrespective of datatype. 100==100 means both are int values and the other condition, 100 == "100" means int comparison with "100" string type of variable still returns true. It means == is not doing a strict type check.

  1. <h2>Difference between =, == and === in Javascript</h2>  
  2. <script type="text/javascript">  
  3.     function Comparision() {  
  4.         var number = 100;  // Here number variable assigned using =  
  5.         debugger;  
  6.   
  7.         if (number === 100)  // Here Comparision between two values using ==. This will not check datatype irrespective of datatype it 
  8.                               will do comparision  
  9.             $("#lblMessage").text("Both are equal");  
  10.         else  
  11.             $("#lblMessage").text("Both are not equal");  
  12.   
  13.         if (number === "100")  // Here Comparision between two values using ==. This will not check datatype irrespective of datatype it                                  will do comparision  
  14.             $("#lblMessage1").text("Both are equal");  
  15.         else  
  16.            $("#lblMessage1").text("Both are not equal");  
  17.     }  
  18. </script>  
  19. <table>  
  20.     <tr>  
  21.         <td>  
  22.             100 === 100  
  23.         </td>  
  24.         <td>  
  25.            <label id="lblMessage" runat="server" ></label>  
  26.         </td>  
  27.     </tr>  
  28.     <tr>  
  29.         <td>  
  30.             100 === "100"  
  31.         </td>  
  32.         <td>  
  33.            <label id="lblMessage1" runat="server" ></label>  
  34.         </td>  
  35.     </tr>  
  36.       
  37. </table>  
  38.  <button id="btnSubmit" type="submit" onclick="Comparision();" class="btn btn-primary">  
  39.                 Submit</button>  
Difference between =, == and === in JavaScript

In the above code snippet, I have compared two variables using === operator. It returns true for 100 === 100 and it returns false for 100 === "100". It means === does a strict check for comparison. It checks datatype also and does a comparison based on it.

Consclusion

Here, I have tried to explain the concept of comparison of two varibale using == and === operator. The single = is used for assigning the value to variable and == , === are used for comparison purposes. == compares two variables irrespective of data type while === compares two variables in a strict check, which means it checks for data type also then it returns true or false.