This is an ongoing series about weird things I’ve run across in JavaScript. For part one, we’ll talk about === vs. ==.

Three equal signs is unique to a few languages, including JavaScript. When you have two equal signs like this:

2 == 2;

You are testing if each item is equal to the other, you can use two equal signs. The above expression would be true. But with three equal signs, it also tests the type.

2 === 2;

This would also evaluate to true, but let’s take a look at some of the weird things you can do with two equal signs as compared to three equal signs.

'2' == 2;
'2' === 2;

The first example will evaluate to true, even though the first two is in a string. This is because two equal signs does not check for type, in fact it will completely ignore the type if the contents of the string are equal to the number. This can cause some weird things to happen, and we’ll get to that in a moment.

The second example will not evaluate to true because that third equal sign does test for type. Even though the content of the string is 2, because the types are not the same, they cannot be equal.

Questions? Tweet me!