Difference between == and === with Examples in JavaScript

This is one of the most frequently asked questions in interviews when someone tries to judge your jQuery or JavaScript concepts.
 
JavaScript provides different types of operators. Here, we will be talking about strict equality and Type converting equality.
 
Strict equality (===) means values which we are comparing must have the same type.
 
This means "2" will not be equal to 2 ("2"===2 it will return false)
 
Type converting equality (==) means automatically it will covert the variable to value irrespective of data type; either it is a string or a number. This means "2" will be equal to 2 ("2" == 2 it will return true).
 
So the double equal (==) is an auto type converting equality and three equals  (===) is a strict equality operator, i.e., it will not covert values automatically.
 
diffrence
 
Difference between == and === with Example – JavaScript
 
Below is the demonstration with simple examples:
 
Demo 1
 
1==”1″ // it will return true because here-string will be converted as number
1 === “1” // it will return false because here 1 is number and “1” is string
 
Demo 2
 
0 == false // it will return true because here false is equivalent to 0
0 === false // it will return false because both are different operands
 
I have included the complete example for you to check the result yourself as shown below:
  1. <!DOCTYPE HTML>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Jquery - Difference between == and ===</title>  
  6.     <script type="text/javascript">  
  7.         function CheckDifference()  
  8.     {  
  9.             var val = "2";  
  10.             document.write("Value for a variable is : " + val + "<br/>");  
  11.   
  12.             if (val == 2)  
  13.                 document.write("== returns True <br/>")  
  14.             else  
  15.                 document.write("== returns False <br/>")  
  16.   
  17.             if (val === 2)  
  18.                 document.write("=== returns True <br/>")  
  19.             else  
  20.                 document.write("=== returns False <br/>")  
  21.         }  
  22.     </script>  
  23. </head>  
  24.   
  25. <body>  
  26.     <h2>JQuery or JavaScript - Difference between == and ===</h2>  
  27.     <hr/>  
  28.     <br/>  
  29.     <div id="div1">  
  30.         <input type="button" id="Check" onclick="CheckDifference()" value="Click to Check Difference" />  
  31.     </div>  
  32. </body>  
  33.   
  34. </html>  
Run the example to see live results:
I hope you enjoyed this post and gained knowledge about the difference between the  double and triple equals sign in JavaScript or jQuery.
 
What do you think?
 
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.