Difference between == and === operator in JavaScript
The ‘==’ operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison.
But the ‘===’ operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it will return false.
Example 1:
<script> // In R.H.S. string "9" is converted into // number 9, hence returns true. document.write(9 == "9"); // used for next line document.write('<br>') // Here no type conversion takes place, // hence returns false document.write(9 === "9"); </script> |
Output:
true false
Example 2:
<script> // Here L.H.S. is a string literal whereas // R.H.S. is a string object, // due to type conversion of string object into // a string literal, it returns true. document.write("GeeksforGeeks" == new String("GeeksforGeeks")); // used for next line document.write('<br>') // No type conversion takes place document.write("GeeksforGeeks" === new String("GeeksforGeeks")); </script> |
Output:
true false
Example 3:
<script> // Here number 1 is converted into true(boolean type) // as in javascript true is referred as 1 and false is // referred as 0, hence it returns true. document.write(true == '1'); // used for next line document.write('<br>') // No type conversion so it returns false document.write(true === '1'); </script> |
Output:
true false
In general “===” operator is recommended since it never does type conversion we are doing an exact comparison thus it always produces correct results.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

