Introduction
Let’s look at a few equality tests:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18console.log(true==1); // true
console.log(1=="1"); // true
console.log(true===1); // false
console.log(1==="1"); // false
var a = [1,2,3];
var b = [1,2,3];
var c = a;
console.log(a==b); // false
console.log(a==c); // true
var d = new String("text");
var e = "text";
console.log(d==e); // true
console.log(d===e); // false
If you understand why the equality comparison operators return what they return above, then turn around and never look back :D. If not then continue reading…
What do ==
and ===
represent?
In Javascript, ==
represents to the equality operator and ===
represents to the identity operator.
I think this topic in Javascript confuses one and all. The rules are too many and too complex to memorize.
To counter this confusion, some people say - When in doubt, ask Javascript.
While the others ask you to - Always use the identity operator instead of the equality operator.
And then, there are a few nasty people who ask you to go through the documentation :D.
So then, how do we get rid of this confusion? We don’t. We test it on Javascript when we are confused and we try to use the identity operator ===
unless we really really have to resort to using its evil twin (the ==
equality operator).
Here are a few basic rules that you should remember:
The
==
operator does the necessary type conversion before comparison. For eg:1
2=="2" //returns true
The
===
operator does no type conversion and directly compares. For eg:1
2==="2" //returns false
And of course, to understand how the ==
equality operator functions, you have to know what Truthy and Falsy values are in Javascript
Reference Types
For reference types - ==
and ===
behave the same (except in one special case).
1 | var a = [1,2,3]; |
The special case is:
1 | var a = "abc"; // a String literal |
A String literal comparison with a String object comparison is a bit tricky. Hopefully the above example helps you to handle such comparisons.
“===” DOES NOT mean “equal and of the same type”
The identity operator ===
DOES NOT always mean equal and of the same type
.
The general rule to follow for indentity comparisons is:
For value types (numbers):
a === b
returnstrue
ifa and b have the same value and are of the same type
For reference types:
a === b
returnstrue
ifa and b reference the exact same object
For strings:
a === b
returnstrue
ifa and b are both strings and contain the exact same characters
Interesting Examples:
There are a few interesting examples that you should be aware of. Just have a glance at the examples mentioned below. You should be able to understand most of them if you understood the article.
1 | '' == '0' // false |
Closing Thoughts
I know this is very confusing but I hope that after reading this article, the MDN documentation and this Stackoverflow question will answer most of your questions.
Note: Most of the examples mentioned in this article are from the Stackoverflow question linked above. They were exceptionally good examples and I thought it would help more if I used the same examples.