Difference between == and === in Javascript

In Javascript when Comparing things, you will tend to use either double equals or triple equals. They are referred to as Equals (==) or Strict Equals (===). Javascript is a typeless language. Its purpose was designed for basic operations, not complex operations you can do in languages such as C# or C++.

If you did the following code snippet in Javascript, then it would write the value Match and NoMatch on the page. WORD will not appear whether you do double or triple equals.

if(1=='1') document.write('Match ')
if(1!==='1') document.write('NoMatch ')
if('word'=='WORD') document.write('WORD ')

Below, I have included the code as a script; you will see Match appear. You can see the code by pressing Ctrl+U. I have minified the code, so it might not be easy to see where the script is. If doing not strict equals, you replace the first equals sign with an exclamation mark as above.

WORD doesn't appear because using double equals isn't case sensitive comparison. To make it a case-insensitive comparison, you need to use toUpperCase() or toLowerCase() functions, as shown below.

if(('WORD').toLowerCase() == 'word') document.write('Match');

In most cases, double equals are sufficient, but when you mix types, that is, numbers and words and need them to be different, you should use triple equals.

Tags - Javascript

Last Modified : June 2023


About... / Contact.. / Cookie...